Python Virtual Environment Management Cheat Sheet

For Python Environments Using virtualenv and pip

Install virtualenv (if not installed):

pip install virtualenv

Create a new environment:

virtualenv myenv

Activate the environment:

On Windows:

myenv\Scripts\activate

On macOS and Linux:

source myenv/bin/activate

Deactivate the environment:

deactivate

Install packages:

pip install package_name

List installed packages:

pip list

Delete the environment:

Simply delete the environment folder:

On Windows:

rmdir /s /q myenv

On macOS and Linux:

rm -rf myenv

 

For Python Environments Using conda

Install Miniconda or Anaconda (if not installed).

Visit the official site for installation instructions.

Create a new environment:

conda create --name myenv

Activate the environment:

conda activate myenv

Deactivate the environment:

conda deactivate

Install packages:

conda install package_name

List environments:

conda env list

List installed packages in an environment:

conda list

Delete an environment:

conda remove --name myenv --all

Export environment to a YAML file (for sharing or version control):

conda env export > environment.yml

Create an environment from a YAML file:

conda env create -f environment.yml