python 导入错误

发布于 2024-09-15 05:34:41 字数 644 浏览 8 评论 0原文

我的进口有什么问题吗?

应用程序文件夹结构:

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__.pymyapp/models/__init__.pymyapp/tests/__init__ 中有 __init__.py 文件.py 并且如上所述,使用nosetests 运行单元测试可以按预期工作。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷雾森÷林ヴ 2024-09-22 05:34:41

它是 __init__.py 而不是 init.py。确保层次结构中的每个目录都包含它以便能够导入。

编辑:我成功地复制了它。
这是目录结构:

cesar@cesar-laptop:/tmp/asdasd$ tree
.
`-- myapp
    |-- __init__.py
    |-- models
    |   |-- __init__.py
    |   `-- models.py
    |-- scripts
    |   |-- data.py
    |   `-- __init__.py
    `-- tests
        |-- __init__.py
        `-- tests.py

我将以下代码放在 data.py 的最开头以缩小问题范围:

import sys
import pprint

pprint.pprint(sys.path)

from myapp.models.models import *

按照 OP 指示的方式运行 data.py 会产生 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

但是这种方式就像一个魅力:

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']

注意 sys.path 第一个条目的差异。

It is __init__.py not init.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:

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.py to narrow down the problem:

import sys
import pprint

pprint.pprint(sys.path)

from myapp.models.models import *

Running the data.py the way OP indicated 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.

疯到世界奔溃 2024-09-22 05:34:41

你是如何执行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 in sys.path) for you to be able to import 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 the PYTHONPATH environment variable, or adding it to sys.path in sitecustomize.py), you will need to be in the directory containing myapp when you run the script. If you're inside the myapp directory itself, you won't be able to import the myapp package.

旧人哭 2024-09-22 05:34:41

当您运行测试和运行脚本时,您的 sys.path 显然没有以相同的方式设置。 执行此操作

import sys
print(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. Do

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.path the parent directory of myapp in the case where it's missing.

安静被遗忘 2024-09-22 05:34:41

除非您忘记提及,否则 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 in myapp/scripts, so myapp/scripts/import.py is not a module of myapp.

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 into myapp or into its parent folder) or install myapp into your path (i.e. move it to your packages folder, e.g. /usr/lib/python2.6/dist-packages/).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文