How to Install Playwright with Conda in Python


Playwright is a powerful tool for automating web browsers in Python. It allows you to automate tasks such as browsing, clicking, scrolling, and filling out forms. One of the best ways to install Playwright is by using the conda package manager.

Conda is a popular package manager that can be used to install Python packages and manage virtual environments. It is particularly useful for managing dependencies and resolving conflicts between packages.

Using conda to install Playwright will make it easier to manage the dependencies and versions of the packages you need. With this method, you can easily switch between different versions of Playwright and other packages without affecting other projects on your system.

If you don’t have conda installed on your system, you can download and install it from the Miniconda website.

1. Create an environment.yml

Let’s create an environment.yml that declares dependencies on python and playwright.

Here, our conda environment will be called env_name.

name: env_name
channels:
- conda-forge
- microsoft
- defaults
dependencies:
- python=3.10
- playwright

2. Create the conda environment

Let’s create a new environment using conda env create. We can create the environment from our yml file using the -f flag.

This command will create the environment and install the specified dependencies.

conda env create -f environment.yml

Next, let’s activate this new environment.

conda activate env_name

3. Install Playwright

Finally, we can install the browser binaries for Chromium, Firefox, and WebKit.

playwright install

4. Write a basic Playwright Python script

Let’s write a basic script to open up https://logfetch.com and print the HTML content to the terminal.

def main():
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch()
        page = browser.new_page()
        page.goto("https://logfetch.com")
        print(page.content())

if __name__ == "__main__":
    main()

5. Run the Python script

We can invoke this script by running python3.

python3 main.py

ModuleNotFoundError: No module named ‘playwright’

If you run the issue where playwright doesn’t exist, ensure that you’ve activated the appropriate conda environment.

If the problem persists, ensure you’re invoking your Python script using python3 and not python.