Install and run pytest in a virtual environment
Create an isolated test environment, install the declared dependencies, and point terminal, IDE, and CI runs at the same Python interpreter.
Create a virtual environment, install pytest and the project's test dependencies there, then run pytest through that environment's Python. Using python -m pytest ties the runner to the interpreter you selected.
Create and activate the environment
For a POSIX shell:
python3 -m venv .venv
source .venv/bin/activate
python -m pip install pytest
python -m pip install -r requirements.txt
python -m pytest
The requirements step assumes that file exists; use the project's actual dependency workflow. If pytest is already declared in a development requirements file or test extra, install that declaration instead of independently choosing another version.
On Windows, create the environment with the intended Python, then use .venv\Scripts\Activate.ps1 in PowerShell or .venv\Scripts\activate.bat in cmd. Activation adjusts the shell's path; it is not required when you call .venv/bin/python or .venv\Scripts\python.exe directly.
Verify the interpreter, not just the command name
python -c "import sys; print(sys.executable)"
python -m pip --version
python -m pytest --version
python -m pytest tests/test_api.py -v
The executable and pip installation should correspond to the intended environment. which pytest on POSIX shells or where.exe pytest on Windows can help diagnose a conflicting executable, but running the explicit environment Python avoids relying on shell command lookup.
A virtual environment isolates packages from the base environment by default. It does not freeze versions automatically or make native system dependencies disappear. Declare the pytest version and plugins alongside the project's test dependencies when consistent team and CI behavior matters.
FAQ
Why is pytest installed but the command is missing?
The installer and shell may be using different environments, or the script directory may not be on PATH. Check sys.executable and use that interpreter with -m pip and -m pytest. Do not assume activation succeeded solely because the prompt changed.
How should an IDE run tests?
Select the environment's Python as the project/test interpreter and ensure pytest is installed there. Also verify the IDE's test framework, working directory, and configuration; choosing an interpreter does not correct every runner setting automatically.
Should pytest be a production dependency?
Usually it belongs with development/test dependencies, such as a requirements-dev.txt file or a suitable optional dependency group. Install the production set for deployment and the test set in CI. Keep both sets compatible and reproducible rather than assuming their separation alone prevents drift.
References: Python virtual environments, Getting started with pytest, Running pytest.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan