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.
📖 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\Scriptson Windows or.venv/binon macOS and Linux; - its own
site-packagesfolder (.venv\Lib\site-packages), where pip installs packages; - a
pyvenv.cfgfile 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.prefixis the root of the active environment. If it points at your project’s.venv, you are in the right place.sys.executableis 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_prefixis the Python the environment was created from. When it differs fromsys.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.
| Command | What it does |
|---|---|
python -m pip install pytest-playwright | Install the latest version and its dependencies |
python -m pip install "pytest-playwright==0.10.0" | Install an exact version |
python -m pip install --upgrade playwright | Upgrade to the newest version |
python -m pip install -r requirements.txt | Install everything listed in a file |
python -m pip list | List installed packages and versions |
python -m pip show playwright | Details of one package, including what it requires |
python -m pip uninstall -y greenlet | Remove a package without asking for confirmation |
python -m pip check | Verify that every package’s dependencies are installed and compatible |
python -m pip index versions pytest-playwright | List the versions available on PyPI |
python -m pip freeze | Print 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 (
greenletandpyee). - 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:
| Line | Meaning |
|---|---|
pytest-playwright | Any version (whatever is newest today) |
pytest-playwright==0.10.0 | Exactly this version |
playwright>=1.64,<2 | At least 1.64 but below 2.0 |
pytest~=9.2 | Compatible 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
- Check what is available:
python -m pip index versions playwright. - Upgrade in your environment:
python -m pip install --upgrade playwright pytest-playwright. - Download the matching browsers:
playwright install(each Playwright version needs its own browser builds). - Run the whole suite. Read the Playwright release notes for behaviour changes.
- Update the pinned versions in
requirements.txtand commit them, so CI upgrades in the same step.
🩺 Common Errors and Fixes
| Symptom | Likely cause | Fix |
|---|---|---|
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 missing | The pytest command belongs to another Python | Use python -m pytest; check sys.executable |
| “Executable doesn’t exist” when a browser launches | Browsers not downloaded for this Playwright version | Run playwright install |
| Works locally, fails in CI after a new release | Unpinned versions | Pin versions in requirements.txt |
| Activation blocked in PowerShell | Script execution policy | Set-ExecutionPolicy -Scope CurrentUser RemoteSigned |
🏋️ Practice Exercises
Try each exercise on your own first, then open the answer to compare. The answers use the same practice sites and project layout as the lesson.
Exercise 1: Write the commands that show which Python is running, list installed packages, and show the installed version of playwright.
Show answer
python -c "import sys; print(sys.executable)"
pip list
pip show playwrightExercise 2: Split dependencies into requirements.txt (runtime) and requirements-dev.txt (adds linting tools) so that the dev file includes the runtime file.
Show answer
# requirements.txt
playwright==1.65.2
pytest==9.2.0
pytest-playwright==0.10.0# requirements-dev.txt
-r requirements.txt
ruff>=0.6pip install -r requirements-dev.txt installs both.
Exercise 3: Describe the safe steps to upgrade Playwright in a project.
Show answer
Read the release notes, then pip install -U playwright pytest-playwright (or change the pins), run playwright install to fetch the matching browsers, run the full suite, fix any failures, and commit the updated requirements.txt together with the code changes.
🧠 MCQ Quiz: Virtual Environments and pip
10 questions on this lesson. Pick one answer for each, then press Check answers to see your score and an explanation for every question.
- What does a virtual environment contain?Answer: Its own Python interpreter link and installed packages. It isolates packages per project.
- How do you check which Python interpreter is active?Answer:
python -c "import sys; print(sys.executable)".sys.executableshows the interpreter path, revealing the active venv. - What does
pip show playwrightdisplay?Answer: Version, location and dependencies of playwright.showprints metadata for one package. - What does
playwright>=1.60,<2mean?Answer: Any version from 1.60 up to, but not including, 2.0. Version specifiers combine lower and upper bounds. - Which command checks installed packages for broken dependencies?Answer:
pip check.pip checkreports incompatible requirements. - What does
-r requirements.txtinside another requirements file do?Answer: Includes the other file’s requirements. Requirements files can include each other. - After upgrading the playwright package, what must you also run?Answer:
playwright install. Each Playwright version needs its matching browser builds. - Should the
.venvfolder be committed to Git?Answer: No; commit requirements files and recreate the venv. Virtual environments are machine-specific and large. pip installputs packages into the system Python instead of the venv. What is the likely cause?Answer: The virtual environment is not activated. Without activation,piprefers to another interpreter.- Which command lists the versions of a package available on PyPI?Answer:
pip index versions playwright.pip index versionsqueries the package index.
Want a longer, timed test? Take the Module 2 mock interview (20 MCQs in 20 minutes).
❓ 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 changesPATH. - Check
sys.prefixandsys.executablewhenever imports behave strangely. - Use
python -m pipfor install, show, list, check and index versions. - Pin exact versions in requirements.txt, keep dev tools in requirements-dev.txt, and re-run
playwright installafter 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