Leszek
Leszek

Reputation: 6598

ModuleNotFoundError for import within the same package while using pytest

My project structure seems to be correct.

setup.py
mypackage/
    __init__.py
    __main__.py
    main.py
    script1.py #import script2 
    script2.py
tests/
    test_script2.py

File script1.py imports script2.py using 'import script2'.

I can run code without errors with following commands:

python mypackage
python mypackage/main.py

Unfortunately, when I try to execute tests using pytest or python -m pytest I get error that there's no module named script2(full message below). I installed my package in editable mode pip install -e .

I'm able to fix this by using imports with package name like import mypackage.script2 as script2 but then, everyone who will clone my repository will have to install package with pip before running it. Otherwise there will error that mypackage is not found.

I'd like to be able to run this code without pip install and have the option to run each script file separately.
Could you suggest me alternative solution?

Repository: pytest-imports-demo

Error message from pytest:

(venv) lecho:~/pytest-imports-demo$ pytest
================================================= test session starts ==================================================
platform linux -- Python 3.6.7, pytest-4.4.1, py-1.8.0, pluggy-0.9.0
rootdir: /home/lecho/pytest-imports-demo
collected 0 items / 1 errors                                                                                           

======================================================== ERRORS ========================================================
________________________________________ ERROR collecting tests/test_script2.py ________________________________________
ImportError while importing test module '/home/lecho/pytest-imports-demo/tests/test_script2.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_script2.py:2: in <module>
    import mypackage.script1 as script1
mypackage/script1.py:1: in <module>
    import script2
E   ModuleNotFoundError: No module named 'script2'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=============================================== 1 error in 0.05 seconds ================================================

Upvotes: 7

Views: 421

Answers (1)

Dmitry Tokarev
Dmitry Tokarev

Reputation: 2089

In the file pytest-imports-demo/mypackage/script1.py importing script2 package should be done either:

from mypackage import script2

or

from . import script2

Also need to add empty __init__.py file to pytest-imports-demo/tests/ directory.

As far as "I'd like to be able to run this code without pip install and have the option to run each script file separately." goes this can be done by making scripts executable and providing full path to the scripts or putting path to the directory with these scripts into your $PATH environment variable. Alternatively it can be done via pip install (but additional settings are required in setup.py file).

But tests can be run without having to pip install your package.

I opened PR: https://github.com/lecho/pytest-imports-demo/pull/1

Upvotes: 3

Related Questions