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

前端 未结 20 2353
孤独总比滥情好
孤独总比滥情好 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:27

    I had a similar issue. pytest did not recognize a module installed in the environment I was working in.

    I resolved it by also installing pytest into the same environment.

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

    I started getting weird ConftestImportFailure: ImportError('No module named ... errors when I had accidentally added __init__.py file to my src directory (which was not supposed to be a Python package, just a container of all source).

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

    I was having the same problem when following the Flask tutorial and I found the answer on the official Pytest docs It's a little shift from the way I (and I think many others) are used to do things.

    You have to create a setup.py file in your project's root directory with at least the following two lines:

    from setuptools import setup, find_packages
    setup(name="PACKAGENAME", packages=find_packages())
    

    where PACKAGENAME is your app's name. Then you have to install it with pip:

    pip install -e .
    

    The -e flag tells pip to intall the package in editable or "develop" mode. So the next time you run pytest it should find your app in the standard PYTHONPATH.

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

    According to a post on Medium by Dirk Avery (and supported by my personal experience) if you're using a virtual environment for your project then you can't use a system-wide install of pytest; you have to install it in the virtual environment and use that install.

    In particular, if you have it installed in both places then simply running the pytest command won't work because it will be using the system install. As the other answers have described, one simple solution is to run python -m pytest instead of pytest; this works because it uses the environment's version of pytest. Alternatively, you can just uninstall the system's version of pytest; after reactivating the virtual environment the pytest command should work.

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

    I got this error as I used relative imports incorrectly. In the OP example, test_app.py should import functions using e.g.

    from repo.app import *
    

    However liberally __init__.py files are scattered around the file structure, this does not work and creates the kind of ImportError seen unless the files and test files are in the same directory.

    from app import *
    

    Here's an example of what I had to do with one of my projects:

    Here’s my project structure:

    microbit/
    microbit/activity_indicator/activity_indicator.py
    microbit/tests/test_activity_indicator.py
    

    To be able to access activity_indicator.py from test_activity_indicator.py I needed to:

    • start test_activity_indicatory.py with the correct relative import:
        from microbit.activity_indicator.activity_indicator import *
    
    • put __init__.py files throughout the project structure:
        microbit/
        microbit/__init__.py
        microbit/activity_indicator/__init__.py
        microbit/activity_indicator/activity_indicator.py
        microbit/tests/__init__.py
        microbit/tests/test_activity_indicator.py
    
    0 讨论(0)
  • 2020-11-22 07:36

    Yes, the source folder is not in Python's path if you cd to the tests directory.

    You have 2 choices:

    1. Add the path manually to the test files, something like this:

      import sys, os
      myPath = os.path.dirname(os.path.abspath(__file__))
      sys.path.insert(0, myPath + '/../')
      
    2. Run the tests with the env var PYTHONPATH=../.

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