标签归档:nose

Python鼻子导入错误

问题:Python鼻子导入错误

我似乎无法获得鼻子测试框架来识别文件结构中测试脚本下的模块。我设置了最简单的示例来演示该问题。我会在下面解释。

这是包文件的结构:

./__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应该将所有父包都添加到路径以及调用它的目录中,但是在我看来,这似乎没有发生。

我正在使用Python 2.6.2运行Ubuntu 8.04。如果重要的话,我已经手动构建并安装了鼻子(不使用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.


回答 0

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

如果不删除它,则必须将其更改importimport 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.


回答 1

您在虚拟环境中吗?就我而言,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

回答 2

对于那些以后会发现此问题的人:如果__init__.py我的tests目录中没有文件,则会出现导入错误。

我的目录结构是这样的:

./tests/
  ./test_some_random_stuff.py

如果我进行了鼻子测试:

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

回答 3

另一个潜在的问题似乎是目录树中的连字符/破折号。我最近通过重命名目录固定鼻子导入错误的问题sub-dirsub_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.


回答 4

当然,如果在导入的模块中存在语法错误,则会导致此错误。对我来说,当我备份一个tests文件时,这个问题浮出水面,该文件的路径与modules / tests.bak.py类似,位于与tests.py相同的目录中。另外,要处理Django应用程序中的init包/模块问题,可以运行以下命令(在bash / OSX shell中)以确保周围没有任何init .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

回答 5

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

傻了,但是发生了。

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

Silly, but happens.


回答 6

我只是碰到另一件事可能导致这个问题:以形式命名测试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.


回答 7

例如,下面的目录结构,如果你想运行nosetestsm1m2m3以测试某些功能n.py,你应该使用from m2.m3 import ntest.py

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

回答 8

只需完成一个问题:如果您正在为这样的结构而苦苦挣扎:

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