How to activate Python venv on a Mac?

Published: at 08:16 PM

In Python, venv or virtalenv helps you handle different Python packages installation for multiple projects. It stands for virtual environment, that lets you create a separate/isolated Python installation, and install different packages on to that virtual installation. It is very handy and easy to use.

Using venv, you can easily work with multiple projects with various dependencies on the same machine at the same time.

To setup virtual environment or venv on Python, first you will need PIP. It is the widely used packet manager for Python.

PIP comes bundled with Python installation. On a Mac, Homebrew makes it easier to install Python along with pip. Simply, 🔥 fire up your terminal and enter the following command:

> brew install python@3.9

The above command installs Python (latest version at the time of the writing) on your Mac. If you already have Python installed on your machine, you can check the version using the following command on you terminal.

> python -V

You can install the latest version of pip using the following. This command will automatically install the latest pip version.

python3.9 -m pip install --user --upgrade pip

Now, its time for you install the venv or virtual environment in Python using the following command.

> python3.9 -m pip install --user virtualenv

Create your virtual environment To create a virtual environment, head to your project directory and run the following command.

> python3.9 -m venv venv

Activating your virtual environment Before using your virtual environment on your project, you need to activate it using

> source venv/bin/activate

Congratulations 🎉, you’ve successfully installed venv and activated it. Install em’ packages now 🛠

A shorter path on modern Python

Everything above works, but if you’re on Python 3.3 or newer — which is anything Homebrew installs today — venv is part of the standard library. You don’t need to pip install virtualenv at all:

> python3 -m venv .venv
> source .venv/bin/activate

Two small habits that pay off:

How to tell it actually worked

Your prompt gets a (.venv) prefix, but the prompt can lie if your shell config overrides it. The reliable check is where the interpreter resolves to:

> which python
/Users/you/projects/my-app/.venv/bin/python

> python -V
Python 3.12.4

Inside an active venv, plain python and pip point at the environment — you no longer need the python3 / pip3 spelling. If which python still shows /usr/bin/python or a Homebrew path, the environment isn’t active.

Leaving and deleting the environment

To step back out:

> deactivate

There’s no uninstall step — a venv is just a directory. Delete it and start over whenever it gets into a weird state:

> rm -rf .venv && python3 -m venv .venv

That’s genuinely the fastest fix for most “it worked yesterday” dependency problems.

Making the environment reproducible

An environment that only exists on your machine isn’t much use to anyone else. Freeze what you installed:

> pip freeze > requirements.txt

And rebuild it elsewhere:

> python3 -m venv .venv && source .venv/bin/activate
> pip install -r requirements.txt

Add .venv/ to your .gitignore. Committing a virtual environment bloats the repo and the absolute paths baked into .venv/bin/ won’t work on anyone else’s machine anyway.

Common snags on a Mac

If you’re setting up Python on a fresh Mac, it’s worth also setting Python 3 as your default so python and pip behave predictably outside a virtual environment too.

ko-fi