Python Setup Guide for Beginners: Install Python, Create a Virtual Environment, and Run pytest

test-automation

Before you can start test automation with Python, you need a working environment. “I installed it but nothing runs” and “what even is venv?” are the two most common blockers for beginners. This guide walks through every step — on both Windows and Mac — one command at a time.

📌 Who This Article Is For

✅ Anyone installing Python for the first time
✅ Those wondering “what is a virtual environment (venv)?”
✅ QA engineers who want to start test automation with Selenium, Playwright, or pytest
✅ People who feel nervous about the command line

📖 What You’ll Be Able to Do After This Guide

✔ Install Python and verify it works (Windows and Mac both covered)
✔ Create a venv and manage dependencies per project
✔ Install libraries with pip
✔ Set up VS Code for a comfortable Python development workflow
✔ Handle common errors (PATH issues, version mismatches, import errors)

I have worked on test automation across multiple projects using Python with Selenium, Playwright, and pytest.
When onboarding new team members, I noticed the same setup mistakes coming up every single time.
This guide is written from that experience to proactively address the exact points where beginners get stuck.

✅ Goal of This Guide

  • Python is installed and python --version shows a version number
  • You can create and activate a venv virtual environment
  • You can install pytest with pip and run your first test

Setup Overview

StepWhat you’ll doEst. time
Install Python5–10 min
Verify the installation1 min
Create a virtual environment with venv2 min
Install libraries with pip2 min
Set up VS Code5 min
Run a smoke test to verify everything works2 min

① Install Python

Windows

1. Download the installer from the Python official site
Open a browser, go to python.org/downloads, and download the Python 3.12.x installer. The very latest version occasionally has libraries that haven’t fully caught up, so the most recent stable 3.12.x release is a safe choice.

2. Run the installer
Launch the downloaded .exe file. Before clicking anything, check the following on the very first screen.

⚠️ Critical: Check “Add Python to PATH” before proceeding
At the bottom of the first installer screen, there is a checkbox labeled Add Python to PATH. Make sure it is checked before clicking “Install Now.”
Skipping this is the #1 reason beginners end up with a Python that seemingly doesn’t work.

3. Open Command Prompt after installation
Search for “cmd” in the Start menu, or press Windows + R, type cmd, and press Enter.

Mac

Mac ships with an old Python version, so it’s best to install a newer one separately for test automation.
Using Homebrew is the most straightforward approach. If Homebrew feels daunting, the official installer at python.org/downloads works equally well.

1. Install Homebrew if you don’t have it
Open Terminal (Finder → Applications → Utilities → Terminal) and run:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

2. Install Python via Homebrew

brew install python

② Verify the Installation

Once installation is complete, confirm it from the command line.

# Run in Command Prompt (Windows) or Terminal (Mac)
python --version

Expected output:

Python 3.12.3
💡 On Mac: you may need to use python3
Mac may have an old Python 2 mapped to the python command. Try python3 --version instead. If a version number appears, you’re good — python3 is perfectly fine to use going forward.

Also verify pip (Python’s package manager):

pip --version
# or on Mac if needed:
pip3 --version

Expected output:

pip 24.0 from /usr/local/lib/python3.12/site-packages/pip (python 3.12)

③ Create a Virtual Environment with venv (Important)

What is venv?

venv is Python’s built-in tool for creating isolated environments per project.

Without it, you run into version conflicts like this:

ProjectRequired version
Project A (new)selenium==4.20
Project B (existing)selenium==4.8

If both projects share a single global Python install, updating selenium for one project breaks the other.
Virtual environments keep each project’s dependencies completely separate.

💡 “Seems like extra work…”
It might feel that way at first, but once you have two or more projects going, you’ll be very glad you set this up. Start the habit now.

Creating Your Virtual Environment

1. Create a project folder and navigate into it

# Create the folder
mkdir my-test-project

# Move into it
cd my-test-project

2. Create the virtual environment

python -m venv venv
# On Mac if python3 is required:
# python3 -m venv venv

A venv folder will be created containing the isolated environment files.
At this point, your project structure looks like this:

my-test-project/
├── venv/              ← virtual environment (do NOT commit to Git)
├── test_sample.py     ← test file (created later)
└── requirements.txt   ← library list (created later)

3. Activate the virtual environment

# Windows (Command Prompt)
venv\Scripts\activate

# Windows (PowerShell)
venv\Scripts\Activate.ps1

# Mac / Linux
source venv/bin/activate

Once activated, your prompt will show (venv) at the start:

(venv) C:\Users\yourname\my-test-project>   # Windows
(venv) yourname@mac my-test-project %       # Mac

While (venv) is visible, the virtual environment is active.
Make it a habit to activate before starting work each time.

4. To deactivate

deactivate

④ Install Libraries with pip

pip is Python’s package manager. Let’s install the libraries commonly used in test automation.
Always make sure your virtual environment is activated (look for (venv)) before running pip.

First, upgrade pip itself (recommended)

python -m pip install --upgrade pip
# pytest (test framework)
pip install pytest

# Selenium (browser automation)
pip install selenium

# Playwright (modern browser automation)
pip install playwright
playwright install  # installs the browser binaries

# requests (API testing)
pip install requests

To see what’s installed in your current environment:

pip list

Managing Dependencies with requirements.txt

When working with a team or reproducing an environment, store your library list in a requirements.txt file.

# Export the current environment to a file
pip freeze > requirements.txt

# Reproduce the environment from the file
pip install -r requirements.txt
💡 pyproject.toml is another option
Many modern Python projects use pyproject.toml for dependency management. For beginners though, requirements.txt is simpler to get started with — learn it first and explore pyproject.toml when you’re comfortable.

⑤ Set Up VS Code

Any editor works, but VS Code (Visual Studio Code) is widely used for Python test automation. It’s free, well-supported, and integrates cleanly with venv.

Installation and Recommended Extensions

Download VS Code from code.visualstudio.com. After installing, add the following extensions (use the Extensions icon in the left sidebar to search and install).

ExtensionPurposePriority
Python (Microsoft)Autocomplete, debugging, venv integration🔴 Required
PylanceHigh-quality type checking and IntelliSense🔴 Required
Error LensShows errors inline next to the code — very helpful for beginners🟡 Recommended
GitLensEnhanced Git support🟡 Recommended

Point VS Code to Your Virtual Environment

After opening your project folder in VS Code, click the Python version shown in the bottom-right corner and select your venv’s Python interpreter.

# Alternatively: open the Command Palette with Ctrl+Shift+P (Mac: Cmd+Shift+P)
# → select "Python: Select Interpreter"
# → choose ./venv/Scripts/python.exe (Windows) or ./venv/bin/python (Mac)

⑥ Run a Smoke Test

Let’s confirm the whole setup is working end to end.

Running Your First pytest

Create a file called test_sample.py in your project folder and paste the following:

# test_sample.py
def test_addition() -> None:
    assert 1 + 1 == 2

def test_string() -> None:
    assert "hello".upper() == "HELLO"

Run it from the terminal (with your virtual environment activated):

# Standard way
pytest test_sample.py -v

# Alternative (useful if pytest command isn't found)
python -m pytest test_sample.py -v

Expected output:

========================= test session starts ==========================
collected 2 items

test_sample.py::test_addition PASSED                            [ 50%]
test_sample.py::test_string   PASSED                            [100%]

========================== 2 passed in 0.12s ==========================

If you see “2 passed” — your environment is ready. 🎉

7 Common Pitfalls in Python Environment Setup

⚠️ Watch out for these

  1. Forgot “Add Python to PATH” on Windows
    This is the most common reason python isn’t recognized. Uninstall Python, reinstall it, and this time check the box.
  2. Running pip install without activating venv
    If (venv) isn’t showing in your prompt, pip installs go to the global Python instead. Run source venv/bin/activate (Mac) or venv\Scripts\activate (Windows) first.
  3. Mixing python and python3 on Mac
    Mac may map python to an old version. Use python3 and pip3 consistently, or use the official installer from python.org to avoid the confusion entirely.
  4. PowerShell activation gives a security error
    This is caused by PowerShell’s execution policy. Open PowerShell as Administrator and run Set-ExecutionPolicy RemoteSigned, then try again.
    ⚠️ Note: On corporate or managed machines, changing the execution policy may not be permitted. Check with your IT or security team before proceeding.
  5. pytest command not found
    First check that venv is activated. If the issue persists, use python -m pytest — this explicitly runs pytest through Python and avoids PATH-related issues.
  6. Installed requirements.txt but still getting import errors
    Double-check that your virtual environment is active. In VS Code, verify the interpreter in the bottom-right corner points to the correct venv path.
  7. ModuleNotFoundError when running pytest from a subdirectory
    Always run pytest from the project root — the folder where pytest.ini or pyproject.toml lives. Running from a subfolder often breaks Python’s module resolution.

Frequently Asked Questions (FAQ)

Q. Which Python version should I use?

Python 3.12.x is a solid choice at the time of writing. If the very latest version is available, it’s generally safer to pick one minor version behind — some libraries take a few months to officially support each new release. Always check the compatibility pages for pytest, Selenium, and Playwright with whatever version you choose.

Q. Should I commit the venv folder to Git?

No — the venv folder should not be committed. Add it to .gitignore and share your dependencies through requirements.txt (or pyproject.toml) instead. Anyone cloning the repo can recreate the environment with pip install -r requirements.txt.

Q. Do I need to activate venv every time?

Yes, if you’re running commands manually from the terminal. However, VS Code auto-recognizes the venv once you set the interpreter, so terminals opened inside VS Code usually don’t need a manual activate step.

Q. Should I use pyenv?

pyenv is great when you need to switch between multiple Python versions. That said, pyenv itself can trip up beginners during setup. Start with the official installer or Homebrew, and come back to pyenv once you need to manage multiple versions.

Q. Can I use Anaconda?

Anaconda is well-suited for data science, but for test automation (Selenium, Playwright, pytest) a plain Python + venv setup is simpler and less prone to library compatibility issues. If you already have Anaconda, you can use conda create environments, but a separate plain Python environment dedicated to test automation tends to be cleaner.

Q. Does the editor matter? Do I have to use VS Code?

Not at all. PyCharm (Community edition is free) is equally popular and has excellent Python-specific features. Most test automation engineers use either VS Code or PyCharm — both are fine choices. Pick whichever feels more comfortable and don’t let the editor be a blocker.

Summary

StepKey point
① Install PythonOn Windows: check “Add Python to PATH” — don’t skip it
② Verifypython --version should display a version number
③ venvCreate one per project. (venv) in the prompt means it’s active
④ pipAlways activate venv first. Upgrade pip before installing libraries
⑤ VS CodeInstall Python + Pylance, then select the venv interpreter
⑥ Smoke test“2 passed” from pytest means you’re ready to go

With your environment set up, the next step is learning how to write actual tests — head to the pytest guide or Selenium setup article linked above.

Setup only needs to happen once. After that, you can focus entirely on writing code. Take it one step at a time.

Copied title and URL