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 --version
I'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 uv
Alternatively, you can install uv with:
curl -LsSf https://astral.sh/uv/install.sh | sh
On 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.12
uv 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/activate
Viola! 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 prefect
You 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 3: Conda
Use conda commands to create and manage a virtual environment and the Python packages inside it.
Install Miniconda
Follow the instructions here for your operating system: https://docs.conda.io/en/latest/miniconda.html.
You may be prompted with “Do you wish the installer to initialize Anaconda3 by running conda init?” We recommend “yes”.
The default conda virtual environment is named
base
.You will know you are in a virtual environment because your terminal prompt will have the name of the environment, for example,
(base)
, in parentheses.The default environment is the
base
environment. If you are starting out, you can just use that environment. To isolate dependencies for different projects or to use different software versions, you will want to create and use multiple virtual environments.NOTE: You do not want to have more than a single virtual environment active at the same time - otherwise it can be tricky to know which versions of packages are being installed and used at runtime.
Deactivate current active environment
conda deactivate
Deactivate the current environment.Create a new virtual environment
conda create -n myenv python=3.12
Create a new conda environment namedmyenv
with the latest version of Python from the main conda package channel with Python 3.12 installed. The environment will have very few packages installed.Activate environment
conda activate myenv
Activate themyenv
virtual environment. Unlike the venv/virtualenv virtual environment managers, wherever you navigate in your terminal you will stay in your activated virtual environment.Manage packages in active environment
conda list
List installed packages and versions in the active environment.pip install prefect
Install the prefect package.pip install -U pandas
Install or update the pandas package from PyPI - the Python package manager.-U
specifies to update the specified package and all dependent packages. PyPI often has packages that are not on conda or conda-forge. Packages from conda and PyPI packages generally play together nicely.pip uninstall pandas
Uninstall pandas.Manage environments
conda env list
List conda environments.conda env remove --name myenv
Remove themyenv
environment.