Python virtual environment management can be tricky, but it's essential for isolating dependencies.
You have several options for virtual environment managers. We provide instructions for uv, venv, and conda below.
uv is relatively new, but it's the fastest and becoming very popular. If you don't already have a Python virtual environment manager, we suggest using it.
First, ensure you aren't already in a Python virtual environment. If you see parentheses around a word such as (base) in your terminal prompt, you probably are in a virtual environment. If it's a conda environment, exit it with conda deactivate. If it's a venv environment, exit it with deactivate. Run those commands until you don't see any parentheses in your prompt.
Check that you don't already have uv installed with:
uv --versionI'm using uv 0.5.25 (Homebrew 2025-01-28) for this example.
Install uv on a Mac or Linux machine with homebrew:
brew install uvAlternatively, you can install uv with:
curl -LsSf https://astral.sh/uv/install.sh | shOn a Windows machine you can install uv with:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"For more assistance, see the uv installation docs.
Now that we have uv installed, let's use it.
The ux of uv borrows from the syntax of popular tools.
If you've used venv before, you'll feel right at home, just preface your usual virtual environment creation command with uv and specify the desired Python version - no need to use a tool such as pyenv to install and manage Python versions.
Here's the command to create a virtual environment with Python 3.12:
uv venv --python 3.12uv will find a Python 3.12 version locally if you have one installed. If you don't have a Python 3.12 version installed, uv will fetch and install the latest stable 3.12 version.
Then, follow the handy command line output instructions to activate your virtual environment:
source .venv/bin/activateViola! Your terminal prompt should display the name of a virtual environment in parentheses.
uv is orders of magnitude faster than pip when it comes to installing packages into your virtual environment. And the syntax is familiar if you're used to using pip. For example, to install the prefect package, just throw uv in front of your usual pip command like this:
uv pip install prefectYou upgrade a package and its dependencies, you can use the same -U flag that you'd use with pip:
uv pip install -U prefect
Option 2: venv
Alternatively, you can create virtual environments and install packages with venv and pip.
venv comes installed with Python.
If you need to install Python 3.12, download it from python.org
Commands (assuming you have Python 3.12 installed):
python3.12 -m venv my_envCreatemy_envwith Python 3.12 installed.source my_env/bin/activateActivatemy_envuv pip install pandasInstall pandas into active environmentdeactivateDeactivate current environmentIf you want to install a bunch of packages, you can list them in a file.
requirements.txtis the common name. You can specify the versions if you want.pip install -r requirements.txtInstall the list of packages in requirements.txt.For more speed you can create a venv with uv. With uv installed:
uv venvActivate the virtual environment
On macOS or Linux:
source .venv/bin/activateOn Windows.
.venv\Scripts\activateTo avoid confusion, don't use venv inside an active conda environment. Deactivate the conda environment first.