简体   繁体   English

如何修复 Python 中的“ImportError: No module named ...”错误?

[英]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/projectsys.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.pypath/to放在PYTHONPATH ( sys.path ) 的开头。

Here is a step-by-step solution:这是一个分步解决方案:

  1. 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() 。)

  2. Go to /home/bodacydo/work/project转到/home/bodacydo/work/project
  3. Run 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 的示例解决方案。

  1. Add the following line into your ~/.bashrc or just run it directly:将以下行添加到您的 ~/.bashrc 或直接运行它:

     export PYTHONPATH="$PYTHONPATH:$HOME/.python"
  2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM