python 导入错误
我的进口有什么问题吗?
应用程序文件夹结构:
myapp/
- models/models.py 包含
SpotModel()
- tests/tests.py 包含 TestSpotModel(unittest.TestCase)。 tests.py 导入
from myapp.models.models import *
,其工作方式类似于魅力 - scripts/import.py 包含
from myapp.models.models import *
问题在于 import .py 执行时会导致错误:
ImportError: No module named myapp.models.models
但tests.py 会运行。
我在 myapp/__init__.py
、myapp/models/__init__.py
、myapp/tests/__init__ 中有
并且如上所述,使用nosetests 运行单元测试可以按预期工作。__init__.py
文件.py
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 *
the problem is that import.py when executed results in an error:
ImportError: No module named myapp.models.models
but tests.py runs.
I have __init__.py
files in myapp/__init__.py
, myapp/models/__init__.py
, myapp/tests/__init__.py
and as mentioned, running the unit tests using nosetests works as intended.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它是
__init__.py
而不是init.py
。确保层次结构中的每个目录都包含它以便能够导入。编辑:我成功地复制了它。
这是目录结构:
我将以下代码放在
data.py
的最开头以缩小问题范围:按照 OP 指示的方式运行
data.py
会产生 ImportError :但是这种方式就像一个魅力:
注意 sys.path 第一个条目的差异。
It is
__init__.py
notinit.py
. Make sure each of the directory in hierarchy contains it in order to be able to import.EDIT: I managed to reproduce it.
Here's the directory structure:
I put the following code at the very beginning of the
data.py
to narrow down the problem:Running the
data.py
the way OP indicated yeilds ImportError:But this way works like a charm:
Note the difference in the first entry of
sys.path
.你是如何执行
import.py
的?执行此操作时当前的工作目录是什么?myapp
目录必须位于 Python path 上(即位于sys.path
中列出的目录之一内),以便您能够导入
它。 Python 在解释器启动时自动将当前工作目录添加到路径列表中。因此,如果包含
myapp
的目录未手动添加到路径中(例如使用PYTHONPATH
环境变量,或将其添加到sys.path
> 在sitecustomize.py
中),运行脚本时您需要位于包含myapp
的目录中。如果您位于myapp
目录本身,则无法导入myapp
包。How are you executing
import.py
? What is the current working directory when you do so?The
myapp
directory must be on the Python path (ie. inside one of the directories listed insys.path
) for you to be able toimport
it. Python automatically adds the current working directory to the path list at interpreter startup time.So if the directory that contains
myapp
is not added to the path manually (eg. using thePYTHONPATH
environment variable, or adding it tosys.path
insitecustomize.py
), you will need to be in the directory containingmyapp
when you run the script. If you're inside themyapp
directory itself, you won't be able to import themyapp
package.当您运行测试和运行脚本时,您的 sys.path 显然没有以相同的方式设置。 执行此操作
在两个模块的顶部 以确认这一点。然后,修复错误的目录(如果缺少,则将
myapp
的父目录附加到sys.path
中)。Your
sys.path
is clearly not set the same way when you're running the test and when you're running the script. Doat the top of both modules to confirm that. Then, fix the one that's wrong (by appending to
sys.path
the parent directory ofmyapp
in the case where it's missing.除非您忘记提及,否则
myapp/scripts
中没有__init__.py
,因此myapp/scripts/import.py
不是myapp
的模块。除非
myapp
以某种方式位于您的全局路径中,否则脚本无法找到它,因为它不会在正确的位置查找它。通过向其中添加
__init__.py
使脚本文件夹成为一个模块,将脚本在文件系统层次结构中向上移动(进入myapp
或其父文件夹)或安装 < code>myapp 到您的路径中(即将其移动到您的packages
文件夹,例如/usr/lib/python2.6/dist-packages/
)。Unless you forgot to mention it, you don't have a
__init__.py
inmyapp/scripts
, somyapp/scripts/import.py
is not a module ofmyapp
.Unless
myapp
is in your global path somehow, the script can't find it because it won't be looking for it in the right place.Either make your scripts folder a module by adding a
__init__.py
to it, move the scripts up the filesystem hierarchy (either intomyapp
or into its parent folder) or installmyapp
into your path (i.e. move it to yourpackages
folder, e.g./usr/lib/python2.6/dist-packages/
).