ModuleNotFoundError: No module named ‘pytest’

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!

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)