Skip to content

Instantly share code, notes, and snippets.

@discdiver
Last active February 20, 2025 16:41
Show Gist options
  • Save discdiver/0bb3bf96f02c182f96d45278f9564551 to your computer and use it in GitHub Desktop.
Save discdiver/0bb3bf96f02c182f96d45278f9564551 to your computer and use it in GitHub Desktop.
Common conda and venv commands

Set up and use a Python virtual environment

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.

Option 1: uv

Install uv

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.

Create a Python virtual environment with uv that will stick around for future use

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.

Install packages with uv

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
@discdiver
Copy link
Author

discdiver commented Jan 11, 2020

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_env Create my_env with Python 3.12 installed.

source my_env/bin/activate Activate my_env

uv pip install pandas Install pandas into active environment

deactivate Deactivate current environment

If you want to install a bunch of packages, you can list them in a file. requirements.txt is the common name. You can specify the versions if you want.

pip install -r requirements.txt Install the list of packages in requirements.txt.

For more speed you can create a venv with uv. With uv installed:

uv venv

Activate the virtual environment

On macOS or Linux:
source .venv/bin/activate

On Windows.
.venv\Scripts\activate

To avoid confusion, don't use venv inside an active conda environment. Deactivate the conda environment first.

@discdiver
Copy link
Author

discdiver commented Feb 20, 2025

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 named myenv 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 the myenv 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 the myenv environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment