问题
I'm having problems with my PythonPath on windows XP, and I'm wondering if I'm doing something wrong.
Say that I have a project (created with Pydev) that has an src
directory. Under src
I have a single package, named common
, and in it a single class module, named service.py
with a class name Service
Say now that I have another project (also created with Pydev) with an src
directory and a common package. In the common package, I have a single script, client.py
, that imports service.
So in other words, two separate disk locations, but same package.
I've noticed that even if I set my PYTHONPATH
to include both src directories, the import fails unless the files are both in the same directory. I get the dreaded no module found.
Am I misunderstanding how python resolves module names? I'm used to Java and its classpath hell.
回答1:
If you really must have a split package like this, read up on the module level attribute __path__.
In short, make one of the 'src' directories the main one, and give it an __init__.py that appends the path of other 'src' to the __path__ list. Python will now look in both places when looking up submodules of 'src'.
I really don't recommend this for the long term though. It is kind of brittle and breaks if you move things around.
回答2:
I think in Python you are best off avoiding this problem by providing each package with a unique name. Don't name both packages common
. Then you can import both with something like
import common1.service as cs
import common2.client as cc
回答3:
If you try to import like this:
import src.common.service
Python will look on the Python path for a directory named "src" (or an egg, etc). Once it finds "src", it will not consider another one. If the first "src" doesn't have common and service inside it, then you will get an ImportError, even if another "src" directory in the path does have those things.
来源:https://stackoverflow.com/questions/3346482/pythonpath-hell-with-overlapping-package-structures