[英]How to fix “ImportError: No module named …” error in Python?
What is the correct way to fix this ImportError error?修复此 ImportError 错误的正确方法是什么?
I have the following directory structure:我有以下目录结构:
/home/bodacydo
/home/bodacydo/work
/home/bodacydo/work/project
/home/bodacydo/work/project/programs
/home/bodacydo/work/project/foo
And I am in the directory我在目录中
/home/bodacydo/work/project
Now if I type现在如果我输入
python ./programs/my_python_program.py
I instantly get我立即得到
ImportError: No module named foo.tasks
The ./programs/my_python_program.py
contains the following line: ./programs/my_python_program.py
包含以下行:
from foo.tasks import my_function
I can't understand why python won't find ./foo/tasks.py
- it's there.我不明白为什么 python 找不到
./foo/tasks.py
- 它就在那里。
If I do it from the Python shell, then it works:如果我从 Python shell 执行此操作,则它可以工作:
python
>>> from foo.tasks import my_function
It only doesn't work if I call it via python ./programs/my_python_program.py
script.只有当我通过
python ./programs/my_python_program.py
脚本调用它时它才不起作用。
Python 不会将当前目录添加到sys.path
,而是添加脚本所在的目录。将/home/bodacydo/work/project
到sys.path
或$PYTHONPATH
。
Do you have a file called __init__.py
in the foo directory?你在 foo 目录中有一个名为
__init__.py
的文件吗? If not then python won't recognise foo as a python package.如果不是,那么 python 不会将 foo 识别为 python 包。
See the section on packages in the python tutorial for more information.有关更多信息,请参阅 python 教程中有关包的部分。
A better fix than setting PYTHONPATH
is to use python -m module.path
比设置
PYTHONPATH
更好的解决方法是使用python -m module.path
This will correctly set sys.path[0]
and is a more reliable way to execute modules.这将正确设置
sys.path[0]
并且是执行模块的更可靠方式。
I have a quick writeup about this problem, as other answerers have mentioned the reason for this is python path/to/file.py
puts path/to
on the beginning of the PYTHONPATH
( sys.path
).我有一个关于这个问题的快速文章,因为其他回答者已经提到了这样做的原因是
python path/to/file.py
将path/to
放在PYTHONPATH
( sys.path
) 的开头。
Here is a step-by-step solution:这是一个分步解决方案:
Add a script called run.py
in /home/bodacydo/work/project
and edit it like this:在
/home/bodacydo/work/project
添加一个名为run.py
的脚本并像这样编辑它:
import programs.my_python_program programs.my_python_program.main()
(replace main()
with your equivalent method in my_python_program
.) (用
my_python_program
的等效方法替换main()
。)
/home/bodacydo/work/project
/home/bodacydo/work/project
run.py
run.py
Explanation: Since python appends to PYTHONPATH the path of the script from which it runs , running run.py
will append /home/bodacydo/work/project
.说明:由于 python 将运行它的脚本的路径附加到 PYTHONPATH ,运行
run.py
将附加/home/bodacydo/work/project
。 And voilà , import foo.tasks
will be found.瞧,
import foo.tasks
会被发现。
Example solution for adding the library to your PYTHONPATH.将库添加到 PYTHONPATH 的示例解决方案。
Add the following line into your ~/.bashrc or just run it directly:将以下行添加到您的 ~/.bashrc 或直接运行它:
export PYTHONPATH="$PYTHONPATH:$HOME/.python"
Then link your required library into your ~/.python folder, eg然后将您需要的库链接到您的 ~/.python 文件夹中,例如
ln -s /home/user/work/project/foo ~/.python/
In my mind I have to consider that the foo
folder is a stand-alone library.在我看来,我必须考虑
foo
文件夹是一个独立的库。 I might want to consider moving it to the Lib\\site-packages
folder within a python installation.我可能想考虑将它移动到 python 安装中的
Lib\\site-packages
文件夹。 I might want to consider adding a foo.pth
file there.我可能想考虑在那里添加一个
foo.pth
文件。
I know it's a library since the ./programs/my_python_program.py
contains the following line:我知道它是一个库,因为
./programs/my_python_program.py
包含以下行:
from foo.tasks import my_function
So it doesn't matter that ./programs
is a sibling folder to ./foo
.所以不要紧,
./programs
是同级文件夹./foo
。 It's the fact that my_python_program.py
is run as a script like this:事实上
my_python_program.py
是作为这样的脚本运行的:
python ./programs/my_python_program.py
蟒蛇./programs/my_python_program.py
If you have this problem when using an instaled version, when using setup.py
, make sure your module is included inside packages
如果您在使用安装版本时遇到此问题,请在使用
setup.py
时确保您的模块包含在packages
setup(name='Your program',
version='0.7.0',
description='Your desccription',
packages=['foo', 'foo.bar'], # add `foo.bar` here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.