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

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

    I had the same problem. I fixed it by adding an empty __init__.py file to my tests directory.

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

    As pointed out by Luiz Lezcano Arialdi, the correct solution is to install your package as an editable package.

    Since I am using pipenv, I thought about adding to his answer a step-by-step how to install the current path as an edible with pipenv, allowing to run pytest without the need of any mangling code or loose files.

    You will need to have the following minimal folder structure (documentation):

    package/
        package/
            __init__.py
            module.py
        tests/
            module_test.py
        setup.py
    

    setup.py most have the following minium code (documentation):

    import setuptools
    
    
    setuptools.setup(name='package', # Change to your package name
                     packages=setuptools.find_packages())
    

    Then you just need to run pipenv install --dev -e . and pipenv will install the current path as an editable package (the --dev flag is optional) (documentation).

    Now you shoul be able to run pytest without problems.

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