Python Virtual Environments and pip for Playwright Test Projects

In Lesson 2 you created a virtual environment and installed pytest-playwright with one command. Now it is time to understand what really happens, because most “it works on my machine” problems in Python test projects come from packages and environments. You will learn how a virtual environment works, the pip commands you will use every week, how to pin versions for a team and CI, and how to diagnose the most common errors.

Advertisement

📖 What You’ll Learn: what a virtual environment contains, how to confirm which Python is running, the essential pip commands (install, show, list, uninstall, check, index versions), version specifiers, requirements.txt and a separate dev requirements file, upgrading Playwright safely, and fixing common environment errors.


🧠 What Is Inside a Virtual Environment?

A virtual environment is just a folder (we call it .venv) that contains:

  • a small copy of (or link to) the Python interpreter, in .venv\Scripts on Windows or .venv/bin on macOS and Linux;
  • its own site-packages folder (.venv\Lib\site-packages), where pip installs packages;
  • a pyvenv.cfg file that records which Python it was created from.

Activating the environment only changes your terminal’s PATH, so that typing python, pip, pytest or playwright runs the copies inside .venv. You can also skip activation and call the environment’s Python directly, which is handy in scripts and CI:

⌨️ Terminal

.venv\Scripts\python -m pytest

📝 Note: The browsers are not inside the virtual environment. playwright install puts them in a shared per-user cache (for example %USERPROFILE%\AppData\Local\ms-playwright on Windows), so several environments that use the same Playwright version share one download.


🔍 Which Python Am I Running?

Half of all environment problems come from running a different Python than you think. Ask Python itself:

⌨️ Terminal

python -c "import sys; print(sys.prefix); print(sys.executable)"

Inside a freshly created .venv in C:\pwv\exp9, the output is:

🖥️ Output

C:\pwv\exp9\.venv
C:\pwv\exp9\.venv\Scripts\python.exe
  • sys.prefix is the root of the active environment. If it points at your project’s .venv, you are in the right place.
  • sys.executable is the full path of the running interpreter.
  • python -c "..." runs a one-line program without creating a file.

The same check works from inside a test, which is useful when CI behaves differently from your laptop. Save it as check_env.py:

📄 check_env.py

import sys
from importlib.metadata import version

print("Python:", sys.version.split()[0])
print("Inside a virtual environment:", sys.prefix != sys.base_prefix)
print("playwright:", version("playwright"))
print("pytest-playwright:", version("pytest-playwright"))
  • sys.base_prefix is the Python the environment was created from. When it differs from sys.prefix, you are inside a virtual environment.
  • importlib.metadata.version("name") reads the installed version of any package.

🖥️ Output

Python: 3.15.1
Inside a virtual environment: True
playwright: 1.64.1
pytest-playwright: 0.10.0

🧰 The pip Commands You Will Actually Use

Always run pip as python -m pip .... It guarantees that the pip belongs to the Python you mean.

CommandWhat it does
python -m pip install pytest-playwrightInstall the latest version and its dependencies
python -m pip install "pytest-playwright==0.10.0"Install an exact version
python -m pip install --upgrade playwrightUpgrade to the newest version
python -m pip install -r requirements.txtInstall everything listed in a file
python -m pip listList installed packages and versions
python -m pip show playwrightDetails of one package, including what it requires
python -m pip uninstall -y greenletRemove a package without asking for confirmation
python -m pip checkVerify that every package’s dependencies are installed and compatible
python -m pip index versions pytest-playwrightList the versions available on PyPI
python -m pip freezePrint installed packages as name==version lines

A brand-new environment contains only pip itself:

🖥️ Output

Package Version
------- -------
pip     26.2.2

After installing, pip show explains how packages depend on each other:

🖥️ Output

Name: playwright
Version: 1.64.1
Summary: A high-level API to automate web browsers
Home-page: https://github.com/Microsoft/playwright-python
Author: Microsoft Corporation
License-Expression: Apache-2.0
Location: C:\pwv\exp9\.venv\Lib\site-packages
Requires: greenlet, pyee
Required-by: pytest-playwright
  • Requires lists what playwright needs (greenlet and pyee).
  • Required-by shows that pytest-playwright depends on it.

pip check is a quick health check. In a healthy environment it prints No broken requirements found.. To see it catch a problem, we uninstalled greenlet on purpose:

🖥️ Output

playwright 1.64.1 requires greenlet, which is not installed.

Reinstalling from requirements.txt repaired the environment, and pip check was happy again.


📌 Version Specifiers and requirements.txt

A requirements file lists one package per line, with an optional version rule:

LineMeaning
pytest-playwrightAny version (whatever is newest today)
pytest-playwright==0.10.0Exactly this version
playwright>=1.64,<2At least 1.64 but below 2.0
pytest~=9.2Compatible release: 9.2 or newer, but below 10

For test projects, exact pins (==) are the safest choice: everybody, including CI, runs exactly the versions you tested with. Create requirements.txt:

📄 requirements.txt

pytest-playwright==0.10.0
playwright==1.64.1
pytest==9.2.0

Pin the main packages yourself, or generate a complete list (including dependencies such as greenlet) with python -m pip freeze > requirements.txt. Then anyone can recreate the environment:

⌨️ Terminal

python -m venv .venv
.venv\Scripts\python -m pip install -r requirements.txt
.venv\Scripts\playwright install

Separate runtime and development requirements

Larger projects keep tools that only developers need (linters, formatters, type checkers) in a second file that includes the first one:

🖥️ Output

# requirements-dev.txt
-r requirements.txt
mypy
ruff

The line -r requirements.txt pulls in the main file, so developers run pip install -r requirements-dev.txt while CI can install only requirements.txt. Pin the dev tools with == too, using the versions your team has agreed on.


⬆️ Upgrading Playwright Safely

  1. Check what is available: python -m pip index versions playwright.
  2. Upgrade in your environment: python -m pip install --upgrade playwright pytest-playwright.
  3. Download the matching browsers: playwright install (each Playwright version needs its own browser builds).
  4. Run the whole suite. Read the Playwright release notes for behaviour changes.
  5. Update the pinned versions in requirements.txt and commit them, so CI upgrades in the same step.

🩺 Common Errors and Fixes

SymptomLikely causeFix
ModuleNotFoundError: No module named 'playwright'Running a Python without Playwright (environment not active)Activate .venv, or run .venv\Scripts\python -m pytest
pytest runs, but plugins or options are missingThe pytest command belongs to another PythonUse python -m pytest; check sys.executable
“Executable doesn’t exist” when a browser launchesBrowsers not downloaded for this Playwright versionRun playwright install
Works locally, fails in CI after a new releaseUnpinned versionsPin versions in requirements.txt
Activation blocked in PowerShellScript execution policySet-ExecutionPolicy -Scope CurrentUser RemoteSigned

❓ Frequently Asked Questions

🤔 Can I move or rename a virtual environment folder?

Not reliably: the scripts inside contain absolute paths. Delete it and create a new one; with a requirements file that takes a minute.

🤔 Should I use venv, conda, poetry or uv?

All of them create isolated environments. This course uses the built-in venv and pip because they come with Python and work everywhere. If your team already uses another tool, the Playwright steps are the same.

🤔 Does each project need its own browsers?

No. Browsers live in a shared cache per Playwright version, so projects on the same version reuse them.

🤔 Why python -m pip instead of pip?

python -m pip always runs the pip of that exact Python. A bare pip might belong to a different installation on your PATH.


🏁 Summary

  • A virtual environment is a folder with its own interpreter and site-packages; activation only changes PATH.
  • Check sys.prefix and sys.executable whenever imports behave strangely.
  • Use python -m pip for install, show, list, check and index versions.
  • Pin exact versions in requirements.txt, keep dev tools in requirements-dev.txt, and re-run playwright install after every upgrade.

Next, you will add type hints to your test code so your editor can auto-complete Playwright methods and catch mistakes before you run anything.

🎬 Watch the Playwright tutorial series on YouTube!

Subscribe to SqaToolsByDY • Practice on the SQA Tools Automation Practice Page

Leave a Comment