Usr bin python no module named pytest

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleNotFoundError: No module named ‘xxx’ #2287

ModuleNotFoundError: No module named ‘xxx’ #2287

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)
Читайте также:  Java annotation in abstract class

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.

The text was updated successfully, but these errors were encountered:

nicoddemus added the type: question general question, might be closed after 2 weeks of inactivity label Mar 5, 2017

@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

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

Источник

How To Fix ModuleNotFoundError: No Module Named ‘Pytest’ In Python?

ModuleNotFoundError: No module named 'pytest' in Python

If you are using Pytest to test Python code and accidentally get the “ModuleNotFoundError: No module named ‘pytest’” in Python, let’s read this article carefully. It will help you fix the above error in the simplest way.

What is Pytest?

Unlike most programming languages, Python uses a built-in library for testing called pytest. Pytest is a framework that makes building simple and scalable tests easy.

When does the ModuleNotFoundError: No module named ‘pytest’ in Python occur? And how to fix it?

Import pytest, but not installed

“ModuleNotFoundError: No module named ‘pytest’” in Python appears when we import pytest and run the test while we don’t have the pytest module installed. See the code below for how the error occurs:

# Import pytest but not installed import pytest def sum(a, b): return a + b def test_function(): assert sum(3, 5) == 8
ModuleNotFoundError: No module named 'pytest'

To fix it, install pytest by opening a terminal and typing the following command:

Now all we need to do is importing and using pytest:

import pytest def sum(a, b): return a + b def test_function(): assert sum(3, 5) == 8

Import with the wrong name

Some libraries must capitalize the first letter in Python when importing, but pytest is different. We do not need to capitalize the first letter of the pytest.

# Import wrong name import Pytest def sum(a, b): return a + b def test_function(): assert sum(3, 5) == 8
ModuleNotFoundError: No module named 'Pytest'

Because pytest and Pytest are different modules, so you can fix this error by using import pytest instead of import Pytest.

import pytest def sum(a, b): return a + b def test_function(): assert sum(3, 5) == 8

Install pytest manually but forgot to run setup.py

This error also appears when you install pytest using the package downloaded here. And forgot to run the command:

Below is the complete guide to help you install pytest manually:

After downloading the installation package, open the terminal and type:

The returned result will be the path to the Python installation directory. As below:

You go to the Python310 directory as the above path. Then continue to go to the folder: \Lib\site-packages\ .

Here, you paste the pytest installation file and extract it. After extracting, you will get the folder like this:

You’re mistaken if you think we have successfully installed pytest in this step. Here is an example if you use pytest in this step.

# Haven't run setup.py import pytest def sum(a, b): return a + b def test_function(): assert sum(3, 5) == 8
ModuleNotFoundError: No module named 'pytest'

As you can see, the error ModuleNotFoundError: No module named ‘pytest’ still appears.

To complete the installation, move to the pytest folder and open a terminal, and type:

If you see the following message:

Finished processing dependencies for pytest==7.1.3

That means you have successfully installed it. Now you can use pytest without the “ModuleNotFoundError: No module named ‘pytest’”.

import pytest def sum(a, b): return a + b def test_function(): assert sum(3, 5) == 8

Summary

That’s for the “ModuleNotFoundError: No module named ‘pytest’” in Python. If you have any questions, please leave us a comment. We will respond as possible. Thank you for reading!

Maybe you are interested:

Hi, I’m Cora Lopez. I have a passion for teaching programming languages such as Python, Java, Php, Javascript … I’m creating the free python course online. I hope this helps you in your learning journey.

Name of the university: HCMUE
Major: IT
Programming Languages: HTML/CSS/Javascript, PHP/sql/laravel, Python, Java

Источник

Оцените статью