Python Nose Import Error

后端 未结 9 560
谎友^
谎友^ 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:51

    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

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

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

    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.

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