How to Install Playwright with Python: Step-by-Step Setup
In this lesson you set up everything you need to write and run Playwright tests in Python. By the end you will have Python, VS Code with the Python extension, a virtual environment for the course, and Playwright installed with its browsers.
What you need before you start
A Mac or Windows machine
A terminal (Terminal on Mac, Command Prompt or PowerShell on Windows)
VS Code, installed from code.visualstudio.com with the default options
You don't need any Playwright experience. If you are new to Python itself, that's fine too: this lesson only installs tools.
Step 1: Install Python
On Mac
Go to python.org/downloads and download the installer for the latest release. Run it. On Mac the PATH is set up for you, so there is no checkbox to worry about.
When the installer finishes, open the terminal and check the version:
python3 --versionOn Mac, use python3. The plain python command won't work there.
On Windows
Same starting point: python.org/downloads, then the Windows installer. While you click through, watch for the checkbox that says Add python.exe to PATH. Tick it before you click Install.
Already clicked through and missed it? Open Control Panel > System > Advanced system settings > Environment Variables, and add the Python folder to Path yourself. Then check the version:
python --versionStep 2: Create a project folder and open it in VS Code
Before opening any tool, create one folder for the course. Everything you build from here on lives inside it. You can create it from the terminal or in your file explorer:
mkdir playwright-python-course
cd playwright-python-courseOpen VS Code in that folder with the code command, or use File > Open Folder:
code .Then install the Python extension: open Extensions, search for Python and install the one from Microsoft. It gives you syntax highlighting, debugging and a way to pick your Python interpreter.
Step 3: Create and activate a virtual environment
Open the terminal inside VS Code. Before installing anything else, create a virtual environment. It keeps this course's packages separate from everything else on your machine. You create it once and activate it each time you work on the course.
python3 -m venv venvOn Windows, use python instead of python3. This creates a folder called venv in your project. Now activate it.
Mac:
source venv/bin/activateWindows:
venv\Scripts\activateOnce it's active, you'll see (venv) at the start of your terminal prompt. Look for it every time before you install a package.
Step 4: Install pytest-playwright
With the virtual environment active, install pytest-playwright with pip. It brings in Playwright and the pytest plugin you will run your tests with:
pip install pytest-playwrightSometimes VS Code shows a message saying you may have installed Python packages into your global environment, even when the virtual environment is active. You can dismiss it or click Don't show again. What matters is that (venv) was showing before you ran pip install.
Have a look at the venv folder in the Explorer now. It holds its own copy of Python, its own pip, and every package you install, including pytest-playwright. This is what isolated means: the folder is self-contained and separate from anything else on your machine.
Step 5: Install the browsers
playwright installThis downloads the browser builds Playwright drives: Chromium, Firefox and WebKit. The same steps are in the Playwright for Python getting started guide if you want the official reference.
Check your setup
Before moving on, make sure you can see all of these:
python3 --version (or python --version on Windows) prints a version number
(venv) shows at the start of your terminal prompt
A venv folder sits in your project in the VS Code Explorer
playwright install finished downloading Chromium, Firefox and WebKit without errors
If all four look right, the environment is done. You write and run your first test in the next lesson.
Common setup errors and fixes
python: command not found on Mac. Use python3. On Mac the plain python command doesn't point to the version you installed.
'python' is not recognized on Windows. The Add python.exe to PATH box was skipped. Add the Python folder to Path under Environment Variables, then open a new terminal.
Packages went into your global Python. The virtual environment wasn't active. Run the activate command, check for (venv), then run pip install again.
VS Code warns about the global environment. If (venv) was showing when you installed, you can dismiss it.
Using the course code
All the code for this course is in the playwright-python-course-yt repo on GitHub. To use it instead of an empty folder, clone it and set it up the same way:
git clone https://github.com/anuradha-learn/playwright-python-course-yt.git
cd playwright-python-course-yt
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
playwright installOn Windows, swap in python and venv\Scripts\activate as in Step 3. The requirements.txt file pins the versions used in the course, including Playwright 1.63 and pytest-playwright 0.9.0, so you get the same setup as the videos. The venv folder isn't in the repo, so you always create your own.
Get Started with Playwright Python: the free series
This post is Lesson 1 of my free Playwright Python course on YouTube.
Watch this lesson: Lesson 1 on YouTube
Next lesson: Lesson 2, Your First Playwright Python Test
Watch the full series in order: Playwright with Python playlist
Course page, lessons and code: anuradhaagarwal.com/playwright-python



