Help with pythonpath

O

Observer

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?, or am i
doing something wrong?.
Thanks for your help and excuse my bad english.
 
T

Tim Roberts

Observer said:
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.
 
M

mg

Tim said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top