Python Nose 导入错误
我似乎无法让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您的顶级目录中有一个
__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, yournosetests
should work.If you don't remove it, you'll have to change your
import
toimport dir.foo
, wheredir
is the name of your directory.你在虚拟环境中吗?就我而言,
nosetests
是/usr/bin/nosetests
中的一个,它使用/usr/bin/python
。 virtualenv中的包肯定不会在系统路径中。以下修复了这个问题: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:对于稍后发现这个问题的人:如果我的测试目录中没有
__init__.py
文件,我会收到导入错误。我的目录结构是这样的:
如果我运行nosetests:
它将给出其他人都看到的
ImportError
。如果我添加一个空白的 __init__.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:
If I ran nosetests:
It would give the
ImportError
that everyone else is seeing. If I add a blank__init__.py
file it works just fine:另一个潜在的问题似乎是目录树中的连字符/破折号。我最近通过将目录从
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
tosub_dir
.当然,如果导入的模块中有语法错误,就会导致这种情况。对我来说,当我备份一个测试文件,其路径如 module/tests.bak.py 与tests.py 位于同一目录中时,问题就出现了。另外,要处理 Django 应用程序中的 init 包/模块问题,您可以运行以下命令(在 bash/OSX shell 中)以确保没有任何 init< /strong>.pyc 文件随处可见:
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:
我收到此错误消息是因为我从错误的目录运行
nosetests
命令。愚蠢,但确实发生了。
I got this error message because I run the
nosetests
command from the wrong directory.Silly, but happens.
我刚刚遇到了可能导致此问题的另一件事:以
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.例如,对于以下目录结构,如果您想在
m1
、m2
或m3
中运行nosetests
来测试n.py
中的一些函数,您应该在test.py
中使用from m2.m3 import n
。For example, with the following directory structure, if you want to run
nosetests
inm1
,m2
orm3
to test some functions inn.py
, you should usefrom m2.m3 import n
intest.py
.只是为了完成这个问题:
如果您正在努力解决这样的结构:
也许您想从项目外部的路径运行测试,
将项目路径包含在 PYTHONPATH 中。
将其粘贴到您的 .profile 中。
如果您在虚拟环境下,请将其粘贴到您的 venv 根目录中的 activate 中
Just to complete the question:
If you're struggling with structure like this:
And maybe you want to run test from a path outside the project,
include your project path inside your PYTHONPATH.
paste it inside your .profile.
If you're under a virtual environment, paste it inside the activate in your venv root