Python Nose Import Error

后端 未结 9 558
谎友^
谎友^ 2020-11-28 22:28

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 p

相关标签:
9条回答
  • 2020-11-28 22:37

    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
    
    0 讨论(0)
  • 2020-11-28 22:37

    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
    
    0 讨论(0)
  • 2020-11-28 22:39

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

    Silly, but happens.

    0 讨论(0)
  • 2020-11-28 22:42

    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.

    0 讨论(0)
  • 2020-11-28 22:42

    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
    
    0 讨论(0)
  • 2020-11-28 22:51

    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.

    0 讨论(0)
提交回复
热议问题