pytest-dev/pytest

ModuleNotFoundError: No module named 'xxx'

mloskot opened this issue ยท 11 comments

I'm using Python 3.6 and pytest 3.0.6, on Windows and Linux.

I have noticed a strange issue. Here is a test project which I used to reproduce the problem: https://github.com/mloskot/python-package/

  1. Go to root folder of the project

  2. Run pytest command and observe the error (see failing Travis CI build 1):

E   ModuleNotFoundError: No module named 'pets'
  1. Run python -m pytest command and observe successful run, with all modules under test being found (see Travis CI build 2)

AFAIK, both commands are equivalent.

The only bug report related to ModuleNotFoundError and Python 3.6 I found is #2132 but I'm not sure if this is related in any way.

Actually pytest and python -m pytest are almost the same except for one difference: the latter will put the current directory into sys.path. This is standard Python behavior, because you are executing python, not the pytest executable. This is the reason why pytest -m pytest works, pets is at your repository root and ends up being found by python.

The standard procedure is to use a virtual environment (which travis already sets up for you automatically) and install your project in development mode:

install: 
    - pip install -r requirements.txt
    - pip install -e .

Then both python and pytest will be able to find pets no matter where you execute them from. For that to work though, you will need to finish writing your setup.py file.

HTH, I will close this for now to not let the issue lingering open, but feel to follow up with further questions.

@nicoddemus Thank you for the informative explanation. Consistent behaviour or not, I now understand what is happening, so no further questions.

Hi... I have python 2.7 installed in my system. I installed pytest, that is compatible with this version of python. However, when I tried writing tests, my code keeps crying for compile errors. Also, when I try the command "pytest --version", I see this error:
pytest --version
Traceback (most recent call last):
File "/usr/local/bin/pytest", line 7, in
from pytest import main
ModuleNotFoundError: No module named 'pytest'

Although, the command "which pytest" gives me this result:
/usr/local/bin/pytest

Can someone please help me with this?

@namratha-bharadwaj your error is unrelated and indicates a completely broken pytest installation
did you do a install using sudo pip install? (because that is known to break the world)

@namratha-bharadwaj just to complement what @RonnyPfannschmidt said, the recommended practice is using a virtualenv instead and install pytest and your project there.

Empty script (no test, just an import statement) that has any one of these import pyautogui, pillow, PyTweening, ... will fail to import with the error above. But other modules work, including those installed with pip, for example the requests module works.

  • Mac OS X 10.9.5
  • Python 3.6.5
  • pytest 3.6.2
  • python env created with python3 -m venv env

But on these it works. Same process.

  • Mac OS X 10.12.6 and higher+ (haven't tried anything between these two versions)
  • Python 3.6.5
  • pytest 3.6.2
  • python env created with python3 -m venv env

atomicwrites 1.1.5
Pillow 5.1.0
PyAutoGUI 0.9.36
PyScreeze 0.1.14
PyTweening 1.0.3

Why am I also seeing this strange behavior too. This is my sample test project

https://github.com/scheung38/travelex-flask-aws-docker/

It was working before once inside venv

$HOME/PyCharmProject/flask-aws-docker> $ py.test -v

but worked several times, but now it is also:

============================================================================= test session starts =============================================================================
platform darwin -- Python 3.6.6, pytest-3.6.3, py-1.5.4, pluggy-0.6.0 -- /$HOME/.conda/envs/travelex-flask-aws-docker/bin/python
cachedir: .pytest_cache
rootdir: $HOME/PycharmProjects/travelex-flask-aws-docker, inifile:
plugins: splinter-1.9.1, flask-0.10.0
collected 0 items / 1 errors

=================================================================================== ERRORS ====================================================================================
___________________________________________________________________ ERROR collecting tests/test_example.py ____________________________________________________________________
ImportError while importing test module '/Users/mincheung/PycharmProjects/travelex-flask-aws-docker/tests/test_example.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_example.py:2: in
from src.factory import create_app
E ModuleNotFoundError: No module named 'src'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================================== 1 error in 3.45 seconds ===========================================================================
(travelex-flask-aws-docker) Mins-MacBook-Pro:travelex-flask-aws-docker mincheung$ py.test -v
============================================================================= test session starts =============================================================================
platform darwin -- Python 3.6.6, pytest-3.6.3, py-1.5.4, pluggy-0.6.0 -- /$HOME/.conda/envs/flask-aws-docker/bin/python
cachedir: .pytest_cache
rootdir: /$HOME/PycharmProjects/flask-aws-docker, inifile:
plugins: splinter-1.9.1, flask-0.10.0
collected 0 items / 1 errors

=================================================================================== ERRORS ====================================================================================
___________________________________________________________________ ERROR collecting tests/test_example.py ____________________________________________________________________
ImportError while importing test module '$HOME/PycharmProjects/flask-aws-docker/tests/test_example.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_example.py:2: in
from src.factory import create_app
E ModuleNotFoundError: No module named 'src'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================================== 1 error in 3.37 seconds ===========================================================================
(flask-aws-docker) Mins-MacBook-Pro:flask-aws-docker$ git remote -v
(flask-aws-docker) Mins-MacBook-Pro:flask-aws-docker$ pwd
/$HOME/PycharmProjects/flask-aws-docker
(flask-aws-docker) Mins-MacBook-Pro:flask-aws-docker$ git add .
(flask-aws-docker) Mins-MacBook-Pro:flask-aws-docker$ git commit -am "initial commit"
[master ef844ea] initial commit
10 files changed, 110 insertions(+), 66 deletions(-)
create mode 100644 requirements.txt
create mode 100644 src/init.py
rewrite src/app.py (99%)
create mode 100644 src/countries.feature
create mode 100644 src/factory.py
create mode 100644 src/main.py
create mode 100644 tests/initi.py
create mode 100644 tests/functional_test.py
delete mode 100644 tests/test.py
create mode 100644 tests/test_example.py
(flask-aws-docker) Mins-MacBook-Pro:flask-aws-docker mincheung$ py.test -v
============================================================================= test session starts =============================================================================
platform darwin -- Python 3.6.6, pytest-3.6.3, py-1.5.4, pluggy-0.6.0 -- /$HOME/.conda/envs/travelex-flask-aws-docker/bin/python
cachedir: .pytest_cache
rootdir: /$HOME/PycharmProjects/flask-aws-docker, inifile:
plugins: splinter-1.9.1, flask-0.10.0
collected 0 items / 1 errors

=================================================================================== ERRORS ====================================================================================
___________________________________________________________________ ERROR collecting tests/test_example.py ____________________________________________________________________
ImportError while importing test module '/$HOME/PycharmProjects/flask-aws-docker/tests/test_example.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_example.py:2: in
from src.factory import create_app
E ModuleNotFoundError: No module named 'src'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================================== 1 error in 3.62 seconds ===========================================================================
(flask-aws-docker) Mins-MacBook-Pro:flask-aws-docker$ pwd

@nicoddemus Thanks for the solution.
My environment is as below:
OS: Windows 10
Python: v3.7
Pytest: v 3.7.1

Solution to problem described:
#########################
I was able to use the below command instead of "py.test"
{point to your project location}>python -m pytest -v -s (followed by the fully qualified path of my test file from the root)

ex:- {point to your project location} = c:\MyProject

 (followed by the fully qualified path of my test file from the root)= c:\MyProject\com\tests\test_one.py

If i execute it from any other location other than {point to your project location}, it throws me error.
@namratha-bharadwaj : May be you can try this out

Actually pytest and python -m pytest are almost the same except for one difference: the latter will put the current directory into sys.path. This is standard Python behavior, because you are executing python, not the pytest executable. This is the reason why pytest -m pytest works, pets is at your repository root and ends up being found by python.

The standard procedure is to use a virtual environment (which travis already sets up for you automatically) and install your project in development mode:

install: 
    - pip install -r requirements.txt
    - pip install -e .

Then both python and pytest will be able to find pets no matter where you execute them from. For that to work though, you will need to finish writing your setup.py file.

HTH, I will close this for now to not let the issue lingering open, but feel to follow up with further questions.

I've created a virtual environment using the command pip install -e . and the problem was fixed. Thanks!

I'm having this same issue within a venv managed by Poetry. python -m "pytest" does work around it, but it's odd that pytest itself, run say via poetry run pytest doesn't work.