How to set up a Python Virtual Environment on Mac, Windows or Linux

Setting up a Python virtual environment is one of the most important skills you can learn as a Python developer. It helps you manage dependencies, avoid version conflicts, and keep your global environment clean — especially when working on multiple projects.

In this guide, I’ll walk you through setting up a virtual environment on macOS, Windows, and Linux with clear, step-by-step instructions.

So what is a Virtual Environment?

A virtual environment is a self-contained directory where Python packages can be installed independently of your system’s global Python setup. It’s perfect for:

  • Keeping dependencies isolated per project
  • Preventing version conflicts
  • Making your projects easier to share and deploy

Prerequisites

    Before you begin, make sure you have:

    • Python 3.3 or later installed
    • pip (Python’s package manager)

    To check the system version of Python you have installed on a Mac open a terminal prompt and run:

    (base) peter@MacBookPro Python % python -V
    Python 3.9.12

    Similarly, to check your pip installation:

    (base) peter@MacBookPro Python % pip -V
    pip 24.0 from /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pip (python 3.9)
    (base) peter@MacBookPro Python % 

    Create a Project Directory

    Create a Directory where you are going to be developing your project and the navigate to it in a terminal or bash window:

    (base) peter@MacBookPro Python % mkdir newdirectory
    (base) peter@MacBookPro Python % cd newdirectory

    Create the Virtual Environment

    Enter the following command to create the virtual environment;

    Bash
    python3 -m venv virtualenvname

    This will create.a subdirectory containing your virtual environment. I tend to call my virtual environments (or ‘virtual envs’) .venv but the choice is yours,

    Activating the Virtual Environment

    Mac OS / Linux

    Before activation

    Bash
    (base) peter@MacBookPro sbs % 

    Activated

    Bash
    (base) peter@MacBookPro sbs % source virtenv/bin/activate<br>(virtenv) (base) peter@MacBookPro sbs % 

    Windows

    Command Prompt

    myenv\Scripts\activate.bat

    Powershell

    PowerShell
    myenv\Scripts\Activate.ps1
    

    Installing Packages

    Now that your environment is active, you can install packages without affecting your global Python installation.

    An example of how to install a package. With the virtual environment (virtenv) activated:

    (virtenv) (base) peter@MacBookPro sbs % pip install requests

    The response I get:

    (virtenv) (base) peter@MacBookPro sbs % pip install requests
    Collecting requests
      Using cached requests-2.32.4-py3-none-any.whl.metadata (4.9 kB)
    Requirement already satisfied: charset_normalizer<4,>=2 in ./virtenv/lib/python3.9/site-packages (from requests) (3.4.2)
    Requirement already satisfied: idna<4,>=2.5 in ./virtenv/lib/python3.9/site-packages (from requests) (3.10)
    Requirement already satisfied: urllib3<3,>=1.21.1 in ./virtenv/lib/python3.9/site-packages (from requests) (2.4.0)
    Requirement already satisfied: certifi>=2017.4.17 in ./virtenv/lib/python3.9/site-packages (from requests) (2025.4.26)
    Using cached requests-2.32.4-py3-none-any.whl (64 kB)
    Installing collected packages: requests
    Successfully installed requests-2.32.4
    
    [notice] A new release of pip is available: 25.1.1 -> 25.2
    [notice] To update, run: pip install --upgrade pip
    (virtenv) (base) peter@MacBookPro sbs % 

    The pip process will install any dependencies which may or may not already be present on your local system so the response you get may vary from mine.

    The key point is you should receive a success message similar to line 10 in the example above.

    Note: whenever you use pip for the first time in a virtual environment, you will receive a notice that your pip version is out of date (lines 12 & 13 in the example). Running an out of date version doesn’t appear to make any difference but you may want to consider upgrading (line 13) if only to remove this annoying message.

    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *