Install Playwright for Python and pytest (Virtual Environment and VS Code Setup)

Before you write a single test you need a clean, repeatable setup. In this lesson you will install Python, create a virtual environment for your test project, install Playwright and pytest with one command, download the browsers, and set up VS Code so it can discover and run your tests.

Advertisement

📖 What You’ll Learn: checking your Python version, creating and activating a virtual environment on Windows, macOS and Linux, installing pytest-playwright, downloading browsers with playwright install, freezing versions in requirements.txt, and running tests from VS Code.


🧰 What You Need

  • Python 3.10 or newer. Playwright 1.64 and pytest-playwright 0.10 both require it.
  • About 1 GB of free disk space for the three browsers.
  • A code editor. This course uses VS Code, which is free.
  • An internet connection for the first install.

You do not need Node.js, Java or a separate WebDriver download.


🐍 Step 1: Install and Check Python

Download Python from python.org/downloads. On Windows, tick “Add python.exe to PATH” on the first installer screen. Then open a new terminal and check the version:

⌨️ Terminal

python --version

On macOS and Linux the command is usually python3:

⌨️ Terminal

python3 --version

You should see Python 3.10 or higher. If you see an older version, or “command not found”, install a current version before continuing.

💡 Tip: In this course, commands use python. If your system only has python3, use that instead everywhere.


📁 Step 2: Create a Project Folder and a Virtual Environment

A virtual environment is a private folder of Python packages for one project. It keeps this project’s Playwright and pytest versions separate from other projects and from the system Python, so upgrading one project never breaks another. You will learn more in Python Virtual Environments and pip.

Create a folder and a virtual environment named .venv inside it:

⌨️ Terminal

mkdir playwright-python-demo
cd playwright-python-demo
python -m venv .venv
  • mkdir creates the project folder and cd moves into it.
  • python -m venv .venv runs Python’s built-in venv module and creates a .venv folder containing a private copy of Python and pip.

Activate the virtual environment

Activation points your terminal’s python and pip at the .venv folder. Use the command for your operating system:

Operating system / shellActivate command
Windows PowerShell.venv\Scripts\Activate.ps1
Windows Command Prompt.venv\Scripts\activate.bat
macOS / Linux (bash, zsh)source .venv/bin/activate

When it works, your prompt starts with (.venv). Run deactivate to leave it.

⚠️ Watch out: If PowerShell says that running scripts is disabled, run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned once, then activate again. This changes the policy only for your user account.


📦 Step 3: Install pytest-playwright

With the virtual environment active, install the official plugin:

⌨️ Terminal

pip install pytest-playwright

This one command installs three things:

  1. pytest-playwright, the plugin that adds the page fixture and Playwright command-line options.
  2. playwright, the browser automation library (installed automatically as a dependency).
  3. pytest, the test runner (also a dependency).

Check the installed versions:

⌨️ Terminal

playwright --version
pytest --version

🖥️ Output

Version 1.64.1
pytest 9.2.0

Your numbers may be newer. That is fine: everything in this course works with Playwright 1.64 and later.


🌐 Step 4: Download the Browsers

The Python package does not contain the browsers themselves. Download the versions that match your Playwright release:

⌨️ Terminal

playwright install

This downloads Chromium, Firefox and WebKit into a shared cache folder (on Windows %USERPROFILE%\AppData\Local\ms-playwright, on macOS ~/Library/Caches/ms-playwright, on Linux ~/.cache/ms-playwright). Every project on your machine that uses the same Playwright version reuses them.

Useful variations:

  • playwright install chromium downloads only Chromium (faster for a first try).
  • playwright install --with-deps also installs the operating-system libraries the browsers need. Use it on Linux and in CI containers; it may ask for administrator (sudo) permission.
  • playwright install --dry-run shows what would be downloaded and where, without downloading.

📝 Note: Each Playwright version needs its own browser builds. After pip install --upgrade playwright, run playwright install again.


✅ Step 5: Verify the Setup with a Test

Create a folder named tests and a file tests/test_setup.py inside it:

📄 tests/test_setup.py

from playwright.sync_api import Page, expect


def test_setup_works(page: Page):
    # Open the SQA Tools practice page
    page.goto("https://sqatools.in/automation-practice-page/")

    # The test passes if the page title matches
    expect(page).to_have_title("Automation Practice Page - SQA Tools")

Run it from the project folder:

⌨️ Terminal

pytest

🖥️ Output

.                                                                        [100%]
1 passed, 1 warning in 1.98s

The single dot means one test passed. pytest found the test because the file name starts with test_ and the function name starts with test_. You will learn these discovery rules in Lesson 4.

📝 Note: With pytest 9.2 and pytest-playwright 0.10.0 you may also see a PytestRemovedIn10Warning about “Session-scoped fixtures defined as instance methods” for the base_url fixture. It comes from inside the plugin, not from your code, and it does not affect your results.


📌 Step 6: Pin Your Versions in requirements.txt

So that teammates and CI install exactly the same versions, save them to a file:

⌨️ Terminal

pip freeze > requirements.txt

pip freeze lists every installed package with its exact version. On a fresh Windows install in November 2026 the file contained these lines (your versions may be newer, and colorama only appears on Windows):

🖥️ Output

colorama==0.4.6
greenlet==3.5.6
iniconfig==2.3.1
packaging==26.3
playwright==1.64.1
pluggy==1.7.0
pyee==14.0.0
Pygments==2.21.0
pytest==9.2.0
pytest-playwright==0.10.0
python-slugify==9.1.3
text-unidecode==1.3
typing_extensions==4.16.0

Anyone can now recreate your environment with:

⌨️ Terminal

pip install -r requirements.txt
playwright install

💡 Tip: Add .venv/ to your .gitignore file. Commit requirements.txt, never the virtual environment folder itself. See our guide to .gitignore.


💻 Step 7: Set Up VS Code

  1. Open the project folder in VS Code (File > Open Folder).
  2. Install the Python extension by Microsoft from the Extensions view (Ctrl+Shift+X).
  3. Press Ctrl+Shift+P, run Python: Select Interpreter, and choose the interpreter inside .venv.
  4. Open the Testing view (the flask icon), click Configure Python Tests, choose pytest, then choose the tests folder.
  5. Your test appears in the tree. Click the ▶ button next to it to run it, or the debug icon to debug it with breakpoints.

📝 Note: The “Playwright Test for VS Code” extension is built for the Node.js test runner (Playwright Test). For Python projects, run and debug tests through the Python extension’s Testing view as shown above.


🗂️ Your Project So Far

🖥️ Output

playwright-python-demo/
├── .venv/                  # virtual environment (not committed)
├── tests/
│   └── test_setup.py       # your first test
└── requirements.txt        # pinned versions

❓ Frequently Asked Questions

🤔 pip says “command not found”. What should I do?

Use python -m pip install pytest-playwright. Running pip through python -m always uses the pip that belongs to the active Python.

🤔 Do I have to install all three browsers?

No. If you only test in Chromium, playwright install chromium is enough. You can add the others later.

🤔 Can I use Google Chrome or Microsoft Edge instead of Playwright’s Chromium?

Yes. Install the browser normally (or with playwright install chrome / playwright install msedge) and run tests with pytest --browser-channel chrome. You will learn this in Lesson 5.

🤔 Is pytest-playwright different from playwright?

Yes. playwright is the automation library. pytest-playwright is the plugin that connects it to pytest. Installing the plugin installs the library for you.


🏁 Summary

  • Use Python 3.10+, and give every test project its own virtual environment.
  • pip install pytest-playwright installs the plugin, Playwright and pytest together.
  • playwright install downloads the matching browsers; add --with-deps on Linux and CI.
  • Freeze versions with pip freeze > requirements.txt, and run tests from VS Code’s Testing view.

Next, you will write a complete Playwright script with sync_playwright to understand browsers, contexts and pages before pytest takes over.

🎬 Watch the Playwright tutorial series on YouTube!

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

Leave a Comment