Python Nose 导入错误

发布于 2024-09-06 05:06:49 字数 1795 浏览 5 评论 0原文

我似乎无法让 nose 测试框架 识别我的测试脚本下的模块文件结构。我已经设置了演示该问题的最简单的示例。下面我会解释一下。

这是包文件结构:

./__init__.py
./foo.py
./tests
   ./__init__.py
   ./test_foo.py

foo.py 包含:

def dumb_true():
    return True

tests/test_foo.py 包含:

import foo

def test_foo():
    assert foo.dumb_true()

两个 init.py 文件都是空的

如果我在主目录中运行 nosetests -vv (其中 foo.py 是),我得到:

Failure: ImportError (No module named foo) ... ERROR

======================================================================
ERROR: Failure: ImportError (No module named foo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python/site-packages/nose-0.11.1-py2.6.egg/nose/loader.py", line 379, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/lib/python/site-packages/nose-0.11.1-py2.6.egg/nose/importer.py", line 39, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/lib/python/site-packages/nose-0.11.1-py2.6.egg/nose/importer.py", line 86, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/home/user/nose_testing/tests/test_foo.py", line 1, in <module>
    import foo
ImportError: No module named foo

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (errors=1)

当我从tests/目录中运行时,我得到同样的错误。根据文档和我发现的示例,nose应该添加所有父包到路径以及调用它的目录,但在我的情况下似乎没有发生这种情况。

我正在运行 Ubuntu 8.04 和 Python 2.6.2。如果重要的话,我已经手动构建并安装了nose(不是使用setup_tools)。

I can't seem to get the nose testing framework to recognize modules beneath my test script in the file structure. I've set up the simplest example that demonstrates the problem. I'll explain it below.

Here's the the package file structure:

./__init__.py
./foo.py
./tests
   ./__init__.py
   ./test_foo.py

foo.py contains:

def dumb_true():
    return True

tests/test_foo.py contains:

import foo

def test_foo():
    assert foo.dumb_true()

Both init.py files are empty

If I run nosetests -vv in the main directory (where foo.py is), I get:

Failure: ImportError (No module named foo) ... ERROR

======================================================================
ERROR: Failure: ImportError (No module named foo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python/site-packages/nose-0.11.1-py2.6.egg/nose/loader.py", line 379, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/lib/python/site-packages/nose-0.11.1-py2.6.egg/nose/importer.py", line 39, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/lib/python/site-packages/nose-0.11.1-py2.6.egg/nose/importer.py", line 86, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/home/user/nose_testing/tests/test_foo.py", line 1, in <module>
    import foo
ImportError: No module named foo

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (errors=1)

I get the same error when I run from inside the tests/ directory. According to the documentation and an example I found, nose is supposed to add all parent packages to the path as well as the directory from which it is called, but this doesn't seem to be happening in my case.

I'm running Ubuntu 8.04 with Python 2.6.2. I've built and installed nose manually (not with setup_tools) if that matters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

软糯酥胸 2024-09-13 05:06:49

您的顶级目录中有一个__init__.py。这使它成为一个包。如果删除它,您的 nosetests 应该可以工作。

如果您不删除它,则必须将 import 更改为 import dir.foo,其中 dir 是您的名称目录。

You've got an __init__.py in your top level directory. That makes it a package. If you remove it, your nosetests should work.

If you don't remove it, you'll have to change your import to import dir.foo, where dir is the name of your directory.

内心旳酸楚 2024-09-13 05:06:49

你在虚拟环境中吗?就我而言,nosetests/usr/bin/nosetests 中的一个,它使用 /usr/bin/python。 virtualenv中的包肯定不会在系统路径中。以下修复了这个问题:

source myvirtualenv/activate
pip install nose
which nosetests
/home/me/myvirtualenv/bin/nosetests

Are you in a virtualenv? In my case, nosetests was the one in /usr/bin/nosetests, which was using /usr/bin/python. The packages in the virtualenv definitely won't be in the system path. The following fixed this:

source myvirtualenv/activate
pip install nose
which nosetests
/home/me/myvirtualenv/bin/nosetests
心病无药医 2024-09-13 05:06:49

对于稍后发现这个问题的人:如果我的测试目录中没有 __init__.py 文件,我会收到导入错误。

我的目录结构是这样的:

./tests/
  ./test_some_random_stuff.py

如果我运行nosetests:

nosetests -w tests

它将给出其他人都看到的ImportError。如果我添加一个空白的 __init__.py 文件,它就可以正常工作:

./tests/
  ./__init__.py
  ./test_some_random_stuff.py

To those of you finding this question later on: I get the import error if I don't have an __init__.py file in my tests directory.

My directory structure was like this:

./tests/
  ./test_some_random_stuff.py

If I ran nosetests:

nosetests -w tests

It would give the ImportError that everyone else is seeing. If I add a blank __init__.py file it works just fine:

./tests/
  ./__init__.py
  ./test_some_random_stuff.py
霊感 2024-09-13 05:06:49

另一个潜在的问题似乎是目录树中的连字符/破折号。我最近通过将目录从 sub-dir 重命名为 sub_dir 修复了鼻子导入错误问题。

Another potential problem appears to be hyphens/dashes in the directory tree. I recently fixed a nose ImportError issue by renaming a directory from sub-dir to sub_dir.

初吻给了烟 2024-09-13 05:06:49

当然,如果导入的模块中有语法错误,就会导致这种情况。对我来说,当我备份一个测试文件,其路径如 module/tests.bak.py 与tests.py 位于同一目录中时,问题就出现了。另外,要处理 Django 应用程序中的 init 包/模块问题,您可以运行以下命令(在 bash/OSX shell 中)以确保没有任何 init< /strong>.pyc 文件随处可见:

find . -name '*.pyc' -delete

Of course if you have a syntax error in the module being imported that will cause this. For me the problem reared its head when I had a backup of a tests file with a path like module/tests.bak.py in the same directory as tests.py. Also, to deal with the init package/module problem in a Django app, you can run the following (in a bash/OSX shell) to make sure you don't have any init.pyc files lying around:

find . -name '*.pyc' -delete
猫瑾少女 2024-09-13 05:06:49

我收到此错误消息是因为我从错误的目录运行 nosetests 命令。

愚蠢,但确实发生了。

I got this error message because I run the nosetests command from the wrong directory.

Silly, but happens.

薄荷港 2024-09-13 05:06:49

我刚刚遇到了可能导致此问题的另一件事:以 testname.test.py 形式命名测试。额外的 . 会让鼻子感到困惑,并导致它导入不应该导入的东西。我想很明显,使用非常规的测试命名约定会破坏事情,但我认为这可能值得注意。

I just ran into one more thing that might cause this issue: naming of tests in the form testname.test.py. That extra . confounds nose and leads to it importing things it should not. I suppose it may be obvious that using unconventional test naming conventions will break things, but I thought it might be worth noting.

转身以后 2024-09-13 05:06:49

例如,对于以下目录结构,如果您想在 m1m2m3 中运行 nosetests 来测试n.py中的一些函数,您应该在test.py中使用from m2.m3 import n

m1
└── m2
    ├── __init__.py
    └── m3
        ├── __init__.py
        ├── n.py
        └── test
            └── test.py

For example, with the following directory structure, if you want to run nosetests in m1, m2 or m3 to test some functions in n.py, you should use from m2.m3 import n in test.py.

m1
└── m2
    ├── __init__.py
    └── m3
        ├── __init__.py
        ├── n.py
        └── test
            └── test.py
吾家有女初长成 2024-09-13 05:06:49

只是为了完成这个问题:
如果您正在努力解决这样的结构:

project
├── m1
├    ├── __init__.py
├    ├── foo1.py
├    └──m2
├       ├── __init__.py
├       └── foo2.py
├
└── test
     ├── __init__.py
     └── test.py

也许您想从项目外部的路径运行测试,
将项目路径包含在 PYTHONPATH 中。

export PYTHONPATH=$PYTHONPATH:$HOME/path/to/project

将其粘贴到您的 .profile 中。
如果您在虚拟环境下,请将其粘贴到您的 venv 根目录中的 activate 中

Just to complete the question:
If you're struggling with structure like this:

project
├── m1
├    ├── __init__.py
├    ├── foo1.py
├    └──m2
├       ├── __init__.py
├       └── foo2.py
├
└── test
     ├── __init__.py
     └── test.py

And maybe you want to run test from a path outside the project,
include your project path inside your PYTHONPATH.

export PYTHONPATH=$PYTHONPATH:$HOME/path/to/project

paste it inside your .profile.
If you're under a virtual environment, paste it inside the activate in your venv root

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