问题:如何解决Python中的“ ImportError:没有名为…的模块”错误?

解决此ImportError错误的正确方法是什么?

我有以下目录结构:

/home/bodacydo
/home/bodacydo/work
/home/bodacydo/work/project
/home/bodacydo/work/project/programs
/home/bodacydo/work/project/foo

我在目录中

/home/bodacydo/work/project

现在,如果我输入

python ./programs/my_python_program.py

我立刻得到

ImportError: No module named foo.tasks

./programs/my_python_program.py包含以下行:

from foo.tasks import my_function

我不明白为什么python找不到./foo/tasks.py-它在那里。

如果我从Python外壳程序执行此操作,那么它将起作用:

python
>>> from foo.tasks import my_function

只有通过python ./programs/my_python_program.py脚本调用它才行。

What is the correct way to fix this ImportError error?

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:

from foo.tasks import my_function

I can’t understand why python won’t find ./foo/tasks.py – it’s there.

If I do it from the Python shell, then it works:

python
>>> from foo.tasks import my_function

It only doesn’t work if I call it via python ./programs/my_python_program.py script.


回答 0

Python不会将当前目录添加到sys.path,而是将脚本所在的目录添加/home/bodacydo/work/project到。添加到sys.path$PYTHONPATH

Python does not add the current directory to sys.path, but rather the directory that the script is in. Add /home/bodacydo/work/project to either sys.path or $PYTHONPATH.


回答 1

__init__.py在foo目录中是否有一个名为的文件?如果不是,则python不会将foo识别为python包。

有关更多信息,请参见python教程中有关软件包部分

Do you have a file called __init__.py in the foo directory? If not then python won’t recognise foo as a python package.

See the section on packages in the python tutorial for more information.


回答 2

以下是分步解决方案:

  1. 添加一个调用的脚本run.py/home/bodacydo/work/project,并编辑这样说:

    import programs.my_python_program
    programs.my_python_program.main()
    

    main()用中的等效方法替换my_python_program。)

  2. /home/bodacydo/work/project
  3. run.py

说明:由于python将PYTHONPATH附加到其运行脚本的路径,因此 running run.py将附加/home/bodacydo/work/project。和voilàimport foo.tasks将被发现。

Here is a step-by-step solution:

  1. Add a script called run.py in /home/bodacydo/work/project and edit it like this:

    import programs.my_python_program
    programs.my_python_program.main()
    

    (replace main() with your equivalent method in my_python_program.)

  2. Go to /home/bodacydo/work/project
  3. Run 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. And voilà, import foo.tasks will be found.


回答 3

将库添加到PYTHONPATH的示例解决方案。

  1. 将以下行添加到〜/ .bashrc或直接运行它:

    export PYTHONPATH="$PYTHONPATH:$HOME/.python"
  2. 然后将所需的库链接到〜/ .python文件夹中,例如

    ln -s /home/user/work/project/foo ~/.python/

Example solution for adding the library to your PYTHONPATH.

  1. Add the following line into your ~/.bashrc or just run it directly:

    export PYTHONPATH="$PYTHONPATH:$HOME/.python"
    
  2. Then link your required library into your ~/.python folder, e.g.

    ln -s /home/user/work/project/foo ~/.python/
    

回答 4

一个更好的比设定的修复PYTHONPATH方法是使用python -m module.path

这将正确设置,sys.path[0]并且是执行模块的更可靠方法。

我有一个快速的书面记录这个问题,其他的应答者提到的原因,因为这是python path/to/file.py看跌期权path/to上的开始PYTHONPATHsys.path)。

A better fix than setting PYTHONPATH is to use python -m module.path

This will correctly set sys.path[0] and is a more reliable way to execute modules.

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).


回答 5

在我看来,我必须考虑该foo文件夹是一个独立的库。我可能要考虑将其移动到Lib\site-packagespython安装中的文件夹中。我可能要考虑在foo.pth此处添加文件。

我知道这是一个图书馆,因为其中./programs/my_python_program.py包含以下行:

from foo.tasks import my_function

因此,这与./programs的同级文件夹无关紧要./foo。这my_python_program.py是作为如下脚本运行的事实:

python ./programs/my_python_program.py

In my mind I have to consider that the foo folder is a stand-alone library. I might want to consider moving it to the Lib\site-packages folder within a python installation. I might want to consider adding a foo.pth file there.

I know it’s a library since the ./programs/my_python_program.py contains the following line:

from foo.tasks import my_function

So it doesn’t matter that ./programs is a sibling folder to ./foo. It’s the fact that my_python_program.py is run as a script like this:

python ./programs/my_python_program.py


回答 6

如果在使用安装版本时遇到此问题,请在使用时setup.py确保模块已包含在其中packages

setup(name='Your program',
    version='0.7.0',
    description='Your desccription',
    packages=['foo', 'foo.bar'], # add `foo.bar` here

If you have this problem when using an instaled version, when using setup.py, make sure your module is included inside packages

setup(name='Your program',
    version='0.7.0',
    description='Your desccription',
    packages=['foo', 'foo.bar'], # add `foo.bar` here

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。