Skip to content

Instantly share code, notes, and snippets.

@justinmklam
Last active May 19, 2021 19:52
Show Gist options
  • Save justinmklam/15556ddcd7a176811237cbd43f4e7c7b to your computer and use it in GitHub Desktop.
Save justinmklam/15556ddcd7a176811237cbd43f4e7c7b to your computer and use it in GitHub Desktop.
Common commands for managing python virtual environments

Managing Multiple Versions of Python

Workflow:

  1. Download the binary of a desired python version
  2. Create a virtual environment that uses the desired python binary

Pyenv (preferred)

# List all available versions
pyenv install --list

# Install a specific version
pyenv install 3.8.9

# Set it globally
pyenv global 3.8.9

# Verify it's correct
which python
# /Users/jlam/.pyenv/shims/python
python --version
# Python 3.8.9

# Required: Install virtualenvwrapper so that pyenv-virtualenvwrapper works
pip install virtualenvwrapper

Linux

To install python3.6 (or others) on Ubuntu 20.04 LTS:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6

Mac

# Search for available versions
brew search python

# Executable will be in /usr/local/opt/python@3.7/bin/python3
brew install python@3.7

Virtual Environments Tools

virtualenvwrapper (preferred)

A thin wrapper around virtualenv that installs all envs in ~/.virtualenv and makes it easier to manage/switch between environments.

# Make a new environment
mkvirtualenv myenv

# Specify a specific python version
mkvirtualenv myenv -p /path/to/bin/python

# Delete an environment
rmvirtualenv myenv

# List the available environments
lsvirtualenv

# Select an environment to use
workon myenv

# Or use the dot if the env name matches the current directory's name (e.g. cd ~/src/myproj; workon myproj)
workon .

virtualenv

virtualenv myenv

# on Windows
source myenv/Scripts/activate

# on Linux
source myenv/bin/activate

Anaconda

For conda > 4.6.7:

# List environments
conda env list

# Create new virtual environment with the current python version
conda create -n myenv python
# Or with a specific python version
conda create -n myenv python=3.6

# Activate env
conda activate myenv

# Deactivate env
conda deactivate

# Remove environment
conda remove --name myenv --all

# Remove environment (alternative command if previous doesn't work
conda env remove --name myenv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment