Python 3:与脚本位于同一目录中的模块:“ImportError:未命名模块”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27365273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python 3: module in same directory as script: "ImportError: No module named"
提问by Arnaud Renaud
I'm trying to import a module (venues
) from an IPython shell. The venues
module is correctly imported but it then tries itself to import a module named makesoup
and fails to do so.
我正在尝试venues
从 IPython shell导入模块 ( )。该venues
模块已正确导入,但随后尝试自行导入名为的模块makesoup
,但未能成功。
I'm located in the ~
directory and am trying to import the venues.py
file located in the subdirectory processors
. The makesoup.py
file is also located in the processors
subdirectory, which means any Python script near it should be able to find it, right?
我位于~
目录中,正在尝试导入venues.py
位于子目录中的文件processors
。该makesoup.py
文件也位于processors
子目录中,这意味着它附近的任何 Python 脚本都应该能够找到它,对吗?
In [1]: import processors.venues
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-765135ed9288> in <module>()
----> 1 import processors.venues
~/processors/venues.py in <module>()
7 """
8
----> 9 import makesoup
10 import re
11
ImportError: No module named 'makesoup'
I have added an empty __init__.py
in boththe ~
and processors
directories, unsuccessfully.
我在和目录__init__.py
中都添加了一个空的,但没有成功。~
processors
Note: the makesoup
module is correctly imported when I'm located in processors
but I know this is not the only way it should work.
注意:makesoup
当我位于时,模块已正确导入,processors
但我知道这不是它应该工作的唯一方式。
采纳答案by Kevin
The
makesoup.py
file is also located in theprocessors
subdirectory, which means any Python script near it should be able to find it, right?
该
makesoup.py
文件也位于processors
子目录中,这意味着它附近的任何 Python 脚本都应该能够找到它,对吗?
No. This feature was changed in Python 3and that syntax no longer works.
否。此功能在 Python 3 中已更改,该语法不再有效。
Change the import makesoup
to this:
改成import makesoup
这样:
from . import makesoup
Or to this:
或者到这个:
from processors import makesoup
Both of these will make it impossible to run python processors/venues.py
directly, though you can still do python -m processors.venues
from your home directory.
python processors/venues.py
尽管您仍然可以python -m processors.venues
从主目录运行,但这两种方法都将无法直接运行。
回答by Newt
Sometimes, this does not work:
有时,这不起作用:
from . import xxx
Maybe someone will tell you to add init.py under the directory. It won't work for some special cases as well.
也许有人会告诉你在目录下添加init.py 。它也不适用于某些特殊情况。
The most useful way would be to check the sys.path first with:
最有用的方法是首先检查 sys.path :
import sys
print(sys.path)
Then you can find where you should import from.
然后你可以找到你应该从哪里导入。
There is an another way as well:
还有另一种方式:
import os
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))
or use insertfunction instead:
或改用插入功能:
sys.path.insert(0, xxx)
These two ways are suitable for small project. I will recommend you choose the first one if your project is complex and huge.
这两种方式适用于小型项目。如果您的项目复杂而庞大,我会建议您选择第一个。