Skip to content

Instantly share code, notes, and snippets.

@valosekj
Last active November 11, 2023 19:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valosekj/8052b227bd3f439a615a33804beaf37f to your computer and use it in GitHub Desktop.
Save valosekj/8052b227bd3f439a615a33804beaf37f to your computer and use it in GitHub Desktop.
How to create and use virtual environments (venv and conda)

How to create and use virtual environments (venv and conda)

venv and conda are environment manager tools allowing to create virtual environments. Virtual environment separates the dependencies (Python packages) for different projects. This mean that each project can have its own dependencies. Usage of virtual environments allows you to avoid installing Python packages globally (to the system Python) which could break system tools or other projects.

venv environment

  1. Create venv

MacOS or Linux (without or with site-packages)

python3 -m venv venv
python3 -m venv venv --system-site-packages

Alternative command on Linux (without or with site-packages)

virtualenv -p /usr/bin/python3 venv
virtualenv -p /usr/bin/python3 --system-site-packages venv
  1. Activate venv
. venv/bin/activate
# or
source venv/bin/activate
  1. Install dependencies

You can install individual packages manually or all required packages specified by requirements.txt file (if the file is provided)

pip install numpy pandas
pip install -r requirements.txt file

Note - pip is aliased to pip3 if you created venv with python3

Now, you can use your venv.

  1. Check all installed packages inside the venv
pip freeze
  1. Deactivate venv
deactivate

Useful aliases (and function) for faster work with venvs

function ce { python3 -m venv venv; echo "venv was created successfully"; if [ -f requirements.txt ]; then echo "requirements.txt file found, installing dependencies..."; source ./venv/bin/activate; pip install -r requirements.txt;fi }
alias ae='deactivate &> /dev/null; source ./venv/bin/activate'
alias de='deactivate'

conda environment

Create conda environment with specific name (flag --name or -n)

conda create --name <env_name>

Create conda environment with python version

conda create -n <env_name> python=3.7

Create conda environment with specific name and python version and automatically install some packages

conda create -n <env_name> pip cython numpy python=3.7

Activate conda environment with specific name

conda activate <env_name>

Install some package (e.g., numpy)

conda install numpy

Some packages require installation using conda-forge channel

# FSLeyes
conda install -c conda-forge fsleyes==1.0.11
# PyTorch
conda install pytorch -c pytorch -c conda-forge

Check all installed packages in certain conda env

conda list

Write all installed packages into txt file

conda list -e > requirements.txt

Create conda env with predefined packages

conda env create -f environment.yaml

Check all created conda environments

conda env list
conda info --envs

Remove given conda venv

conda env remove -n <env_name>

Do not automatically activate base env in each new terminal

conda config --set auto_activate_base false

Activate conda inside shell script

eval "$(conda shell.bash hook)"
conda activate <env_name>

... your bash code ...

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