38

After installing the pytest module in a virtual environment, I used the python code to call and run the prompt to find the pytest module.

I installed the pytest module outside the virtual environment. I can call it normally with python.

import pytest


def test_main():
    assert 5!=5

if __name__ == "__main__":
    pytest.main()

The error is as follows:

[Running] python -u "d:\MyPytest\test_sample.py" Traceback (most recent call last): File "d:\MyPytest\test_sample.py", line 1, in import pytest ModuleNotFoundError: No module named 'pytest' [Done] exited with code=1 in 0.185 seconds

Nathan
  • 7,853
  • 4
  • 27
  • 50
御弟哥哥
  • 481
  • 1
  • 4
  • 3
  • Bumped into the same problem. In my case the problem was that I installed pytest with pip3.9, whereas I built the virtual environment with Python 3.8 and used pip3.8 to install previous packages. So maybe you used different Python versions or pip versions in your virtual environment – Bart Jul 29 '21 at 14:26

5 Answers5

42

TLDR: I suspect you installed pytest within your system level python site-packages so when you try to run pytest, within your virtualenv, it's throwing a ModuleNotFoundError since it doesn't have this dependency installed within your virtualenv. Virtual environments give you a sandboxed environment so you can experiment with potential python libraries for your project, but they're self contained and don't have access to your system level python third-party libraries.

Typically an ImportError is raised when an import statement has trouble successfully importing the specified module. If the problem is due to an invalid or incorrect path, this will raise a ModuleNotFoundError.

From your question it isn't clear where you installed pytest since you said you installed it within your virtualenv then you said you installed it outside your virtualenv on your System level python site-packages.. So I will give my thoughts for getting pytest to work within a virtualenv, since this is probably what you want:

Virtualenv are nice because they give you a sandboxed environment to play around with python libraries, safe from messing up your system level python configurations. Now the ModuleNotFoundError is thrown within your virtualenv because it can't find the pytest module for the test you're trying to run. Maybe you could try activating your virtualenv and re-installing pytest within this virtualenv and seeing if this course of action resolves your issue:

Activate your virtualenv:

# Posix systems
source /path/to/ENV/bin/activate

# Windows
\path\to\env\Scripts\activate

Install pytest within your virtualenv:

Note: you should see your virtualenv's name listed in parenthesizes before installing pytest. For this example, suppose you created a virtual environment named: env

(env) pip install pytest

Now pytest will be available to you within your virtualenv. For more information checkout virtualenv's documentation. I would also suggest looking into virtualenvwrapper, which nicely wraps around virtualenv for more convenient commands to activate/deactivate virtualenvs.

Hopefully that helps!

Nathan
  • 7,853
  • 4
  • 27
  • 50
  • 1
    Hello, I can activate the virtual environment on the command line, but I can't find the module when I run the code runner plugin in vscode. In the vscode, I also activate the virtual environment. – 御弟哥哥 Apr 12 '19 at 14:24
  • So you need to make sure you see pytest within your virtualenv, usually this is located: in your home directory and .virtualenvs folder: ~/.virtualenvs//lib//site-packages if you see it in there then you don't have to install it, if you don't install it in that virtualenv. Then within your visual studio code ide you need to specify to use this virtualenv. Checkout https://code.visualstudio.com/docs/python/environments then you should be able to run your project with the code runner – Nathan Apr 12 '19 at 15:08
  • @御弟哥哥 did my answer help you out? – Nathan Apr 16 '19 at 13:31
12

If it's not the virtualenv case, then try python3 -m pytest.

0

In my case if I use vscode terminal. I get same error. Then I tried same using CMD and its working. So I suggest to use system terminal instead of any third party terminal.

cmd: python -m pytest

S.B
  • 13,077
  • 10
  • 22
  • 49
0

The Python error "ModuleNotFoundError: No module named 'pytest'" occurs for multiple reasons:

  1. Not having the pytest package installed by running pip install pytest.
  2. Installing the package in a different Python version than the one you're using.
  3. Installing the package globally and not in your virtual environment.
  4. Your IDE running an incorrect version of Python.

See the details here.

0

In my case I actually had it installed. Restarting my terminal and re-entering the virtual environment worked for me.