python导入错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3537850/
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 import error
提问by kristian nissen
what's wrong with my imports?
我的进口有什么问题?
App folder structure:
应用文件夹结构:
myapp/
我的应用程序/
- models/models.py contains
SpotModel() - tests/tests.py contains TestSpotModel(unittest.TestCase). tests.py imports
from myapp.models.models import *which works like a charm - scripts/import.py contains
from myapp.models.models import *
- 模型/models.py 包含
SpotModel() - tests/tests.py 包含 TestSpotModel(unittest.TestCase)。tests.py 导入
from myapp.models.models import *就像一个魅力 - 脚本/import.py 包含
from myapp.models.models import *
the problem is that import.py when executed results in an error:
问题是 import.py 在执行时会导致错误:
ImportError: No module named myapp.models.models
but tests.py runs.
但是 tests.py 运行。
I have __init__.pyfiles in myapp/__init__.py, myapp/models/__init__.py, myapp/tests/__init__.pyand as mentioned, running the unit tests using nosetests works as intended.
我__init__.py在myapp/__init__.py、和 中有文件myapp/models/__init__.py,myapp/tests/__init__.py并且如前所述,使用鼻子测试运行单元测试按预期工作。
采纳答案by Ihor Kaharlichenko
It is __init__.pynot init.py. Make sure each of the directory in hierarchy contains it in order to be able to import.
这是__init__.py不是init.py。确保层次结构中的每个目录都包含它以便能够导入。
EDIT:I managed to reproduce it. Here's the directory structure:
编辑:我设法重现它。这是目录结构:
cesar@cesar-laptop:/tmp/asdasd$ tree
.
`-- myapp
|-- __init__.py
|-- models
| |-- __init__.py
| `-- models.py
|-- scripts
| |-- data.py
| `-- __init__.py
`-- tests
|-- __init__.py
`-- tests.py
I put the following code at the very beginning of the data.pyto narrow down the problem:
我把下面的代码放在最开始的地方data.py来缩小问题的范围:
import sys
import pprint
pprint.pprint(sys.path)
from myapp.models.models import *
Running the data.pythe way OP indicated yeilds ImportError:
data.py按照 OP 指示 yeilds ImportError 的方式运行:
cesar@cesar-laptop:/tmp/asdasd$ python myapp/scripts/data.py
['/tmp/asdasd/myapp/scripts',
'/usr/lib/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
-- Skipped --
'/usr/local/lib/python2.6/dist-packages']
Traceback (most recent call last):
File "myapp/scripts/data.py", line 6, in
from myapp.models.models import *
ImportError: No module named myapp.models.models
But this way works like a charm:
但这种方式就像一个魅力:
cesar@cesar-laptop:/tmp/asdasd$ python -m myapp.scripts.data ['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', -- Skipped -- '/usr/local/lib/python2.6/dist-packages']
Note the difference in the first entry of sys.path.
请注意 的第一个条目的差异sys.path。
回答by bobince
How are you executing import.py? What is the current working directory when you do so?
你如何执行import.py?这样做时当前的工作目录是什么?
The myappdirectory must be on the Python path(ie. inside one of the directories listed in sys.path) for you to be able to importit. Python automatically adds the current working directory to the path list at interpreter startup time.
该myapp目录必须位于 Python路径上(即在 中列出的目录之一内sys.path),您才能访问import它。Python 在解释器启动时自动将当前工作目录添加到路径列表中。
So if the directory that contains myappis not added to the path manually (eg. using the PYTHONPATHenvironment variable, or adding it to sys.pathin sitecustomize.py), you will need to be in the directory containing myappwhen you run the script. If you're inside the myappdirectory itself, you won't be able to import the myapppackage.
因此,如果包含的目录myapp未手动添加到路径中(例如,使用PYTHONPATH环境变量,或将其添加到sys.pathin sitecustomize.py),则myapp运行脚本时您将需要位于包含的目录中。如果您在myapp目录内部,则无法导入myapp包。
回答by Alex Martelli
Your sys.pathis clearly not set the same way when you're running the test and when you're running the script. Do
你sys.path显然没有设置,当你运行测试,以及当你运行该脚本的方式相同。做
import sys
print(sys.path)
at the top of both modules to confirm that. Then, fix the one that's wrong (by appending to sys.paththe parent directory of myappin the case where it's missing.
在两个模块的顶部确认。然后,修复错误的那个(通过附加到sys.path的父目录,myapp以防它丢失。
回答by Alan Plum
Unless you forgot to mention it, you don't have a __init__.pyin myapp/scripts, so myapp/scripts/import.pyis not a module of myapp.
除非您忘记提及它,否则您没有__init__.pyin myapp/scripts,因此myapp/scripts/import.py不是myapp.
Unless myappis in your global path somehow, the script can't find it because it won't be looking for it in the right place.
除非myapp以某种方式在您的全局路径中,否则脚本无法找到它,因为它不会在正确的位置寻找它。
Either make your scripts folder a module by adding a __init__.pyto it, move the scripts up the filesystem hierarchy (either into myappor into its parent folder) or install myappinto your path (i.e. move it to your packagesfolder, e.g. /usr/lib/python2.6/dist-packages/).
通过向脚本文件夹添加一个模块__init__.py,将脚本向上移动到文件系统层次结构(进入myapp或进入其父文件夹)或安装myapp到您的路径中(即,将其移动到您的packages文件夹,例如/usr/lib/python2.6/dist-packages/)。

