Python does not recognize a module which is set by PYTHONPATH
我已经找了一段时间来寻找这个问题的解决办法,但都没有找到答案。我希望能在这里找到一些帮助。正如标题所示,python无法识别我的
我目前正在从事一个总体结构如下的项目:
1 2 3 4 5 6 7 | base ├── util │ └── logging_utility.py └── project └── app ├── config.py └── runner.py |
这是在python 3.5中通过名为
my
执行时,我收到此错误:
1 2 3 | Traceback (most recent call last): File"<stdin>", line 1, in <module> ImportError: No module named 'util.logging_utility' |
我从目录的
1 | PYTHONPATH=$HOME/base VIRTUAL_ENV=$HOME/.virtualenvs/venv_1 PATH=$VIRTUAL_ENV/bin:$PATH $HOME/.virtualenvs/venv_1/bin/python -- project/app/runner.py |
现在,我试着通过打印我的
我还尝试将import语句更新为
1 2 3 | Traceback (most recent call last): File"<stdin>", line 1, in <module> ImportError: No module named 'base' |
知道为什么会发生这种情况吗?我该如何纠正?
谢谢!
更新
感谢Billy和Moinuddin Quadri的建议解决了这个问题。出于某种原因,我仍然需要添加
您可能没有在
1 2 | import sys sys.path.append('/absolute/path/to/base') |
因为从python 3.3开始,您不需要
对于旧版本,另一个可能的原因是缺少
如文件包中所述:
The
__init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case,__init__.py can just be an empty file, but it can also execute initialization code for the package or set the__all__ variable, described later.