Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModuleNotFoundError for import within the same package while using pytest

Tags:

python

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 ================================================
like image 499
Leszek Avatar asked Apr 23 '19 10:04

Leszek


People also ask

Why can't I import a module in Python?

The name of the module is incorrect The first reason of this error is the name of the module is incorrect, so you have to check out the module name that you had imported. For example, let's try to import os module with double s and see what will happen: as you can see, we got No module named 'oss'.

How to load the test modules in pytest?

Now pytest will load the modules as tests.foo.test_view and tests.bar.test_view, allowing you to have modules with the same name. But now this introduces a subtle problem: in order to load the test modules from the tests directory, pytest prepends the root of the repository to sys.path, which adds the side-effect that now mypkg is also importable.

What is modulenotfounderror in Python?

A ModuleNotFoundError is raised when Python cannot successfully import a module. The full error message looks something like this: ModuleNotFoundError: No module named 'your_module_name' This error is encountered when you forget to install a dependency for a project.

Why am I getting “modulenotfounderror” when importing via pip?

But when launching the script I received the error: “ModuleNotFoundError” It seems a common problem for many that, when importing via “pip install module_xxx” missing Python modules on a local machine, by default they are not linked with Spyder.


1 Answers

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

like image 65
Dmitry Tokarev Avatar answered Oct 01 '22 23:10

Dmitry Tokarev