PATH issue with pytest 'ImportError: No module named YadaYadaYada'

前端 未结 20 2354
孤独总比滥情好
孤独总比滥情好 2020-11-22 07:10

I used easy_install to install pytest on a mac and started writing tests for a project with a file structure likes so:

repo/
repo/app.py
repo/settings.py
rep         


        
相关标签:
20条回答
  • 2020-11-22 07:37

    For me the problem was tests.py generated by Django along with tests directory. Removing tests.py solved the problem.

    0 讨论(0)
  • 2020-11-22 07:38

    I was getting this error due to something even simpler (you could even say trivial). I hadn't installed the pytest module. So a simple apt install python-pytest fixed it for me.

    'pytest' would have been listed in setup.py as a test dependency. Make sure you install the test requirements as well.

    0 讨论(0)
  • 2020-11-22 07:38

    We have fixed the issue by adding the following environment variable.

    PYTHONPATH=${PYTHONPATH}:${PWD}/src:${PWD}/test
    
    0 讨论(0)
  • 2020-11-22 07:39

    Run pytest itself as a module with: python -m pytest tests

    0 讨论(0)
  • 2020-11-22 07:40

    I'm not sure why py.test does not add the current directory in the PYTHONPATH itself, but here's a workaround (to be executed from the root of your repository):

    python -m pytest tests/
    

    It works because Python adds the current directory in the PYTHONPATH for you.

    0 讨论(0)
  • 2020-11-22 07:40

    conftest solution

    The least invasive solution is adding an empty file named conftest.py in the repo/ directory:

    $ touch repo/conftest.py
    

    That's it. No need to write custom code for mangling the sys.path or remember to drag PYTHONPATH along, or placing __init__.py into dirs where it doesn't belong.

    The project directory afterwards:

    repo
    ├── conftest.py
    ├── app.py
    ├── settings.py
    ├── models.py
    └── tests
         └── test_app.py
    

    Explanation

    pytest looks for the conftest modules on test collection to gather custom hooks and fixtures, and in order to import the custom objects from them, pytest adds the parent directory of the conftest.py to the sys.path (in this case the repo directory).

    Other project structures

    If you have other project structure, place the conftest.py in the package root dir (the one that contains packages but is not a package itself, so does not contain an __init__.py), for example:

    repo
    ├── conftest.py
    ├── spam
    │   ├── __init__.py
    │   ├── bacon.py
    │   └── egg.py
    ├── eggs
    │   ├── __init__.py
    │   └── sausage.py
    └── tests
         ├── test_bacon.py
         └── test_egg.py
    

    src layout

    Although this approach can be used with the src layout (place conftest.py in the src dir):

    repo
    ├── src
    │   ├── conftest.py
    │   ├── spam
    │   │   ├── __init__.py
    │   │   ├── bacon.py
    │   │   └── egg.py
    │   └── eggs 
    │       ├── __init__.py
    │       └── sausage.py
    └── tests
         ├── test_bacon.py
         └── test_egg.py
    

    beware that adding src to PYTHONPATH mitigates the meaning and benefits of the src layout! You will end up with testing the code from repository and not the installed package. If you need to do it, maybe you don't need the src dir at all.

    Where to go from here

    Of course, conftest modules are not just some files to help the source code discovery; it's where all the project-specific enhancements of the pytest framework and the customization of your test suite happen. pytest has a lot of information on conftest modules scattered throughout their docs; start with conftest.py: local per-directory plugins

    Also, SO has an excellent question on conftest modules: In py.test, what is the use of conftest.py files?

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