Re: Help with pythonpath



Tim Roberts wrote:

Observer <observer@xxxxxxxxxxxxxxxx> wrote:


Hi, im a newbie both to python and this list.
I hope someone can help me whith this:

I have a directory structure like this:
.
|-- src
|   `-- pkg
|       |-- __init__.py
|       |-- main.py
|       `-- spkg1
|           |-- __init__.py
|           `-- config.py
`-- src2
  `-- pkg
      |-- __init__.py
      `-- spkg2
          |-- __init__.py
          `-- config2.py

and main.py is a python script that contains this imports:



from pkg.spkg1 import config
from pkg.spkg2 import config2


executed in linux whith this:

env PYTHONPATH=src:src2 src/pkg/main.py
Traceback (most recent call last):
File "src/pkg/main.py", line 4, in ?
  from pkg.spkg2 import config2
ImportError: No module named spkg2

changing the order of the python path only inverts the problem, is there
any way to solve this without changing the directory structure?



Nope. When Python goes to look for a package called "pkg", it starts at
the beginning of PYTHONPATH and stops as soon as it finds one. You either
need to use different names for the two packages (pkg1, pkg2), or use a
symbolic link to link spkg2 into the src directory.


If I remember, I think you need to add an __init__.py file just in your directories src & src2.
Moreover, you can add the path of src and src2 to the variable sys.path at the beginning of your main script (in that case, it not necessary to define PYTHONPATH)


I hope I help you
Mathieu
.


Loading