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
- Create venv
MacOS or Linux (without or with site-packages)
python3 -m venv venv
python3 -m venv venv --system-site-packagesAlternative command on Linux (without or with site-packages)
virtualenv -p /usr/bin/python3 venv
virtualenv -p /usr/bin/python3 --system-site-packages venv- Activate venv
. venv/bin/activate
# or
source venv/bin/activate- 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 pandaspip install -r requirements.txt fileNote - pip is aliased to pip3 if you created venv with python3
Now, you can use your venv.
- Check all installed packages inside the
venv
pip freeze- Deactivate venv
deactivateUseful 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.7Create conda environment with specific name and python version and automatically install some packages
conda create -n <env_name> pip cython numpy python=3.7Activate conda environment with specific name
conda activate <env_name>Install some package (e.g., numpy)
conda install numpySome 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-forgeCheck all installed packages in certain conda env
conda listWrite all installed packages into txt file
conda list -e > requirements.txtCreate conda env with predefined packages
conda env create -f environment.yamlCheck all created conda environments
conda env list
conda info --envsRemove 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 falseActivate conda inside shell script
eval "$(conda shell.bash hook)"
conda activate <env_name>
... your bash code ...
conda deactivate