Python Virtual Environment
Virtual Environments allow Python packages to be installed in an isolated environment instead of installing them globally.
Options:
venv: available by default in Python 3.3 and later
virtualenv: needs to be installed, supports Python 2.7+ and Python 3.3+
conda: package, dependency and environment management for many languages
Recommendation
Use conda to create a development enviroment that requires a specific version of Python and other tools (e.g. C/C++ compiler) and runtime libraries.
Use venv to manage the required Python packages in a project.
Once the virtual environment is created, use conda or pip to install additional Python packages.
Conda
Most used command options
Configuration:
conda config --add channels conda-forge
conda config --set channel_priority strict
conda config --set auto_activate_base false
conda update conda python -y
Creating an enviroment with a specific version of Python:
conda create -n pta-py27 python=2.7
conda create -n pta python=3.10
Installing packages:
conda install jupyter
Get list of installed packages in the current selected environment:
conda list
Get list of installed packages in the current selected environment:
conda list -n pta
Exporting an environment compatible across systems:
conda env export --from-history -f environment.yml
Creating an enviroment from a configuration file:
conda env create -f environment.yml
Updating an enviroment from a configuration file:
conda env update --prune -f enviroment.yml
Removing an environment (you must deactivate the enviroment first):
conda env remove -n pta-py27
Activating/deactiving an enviroment:
conda activate pta
conda deactivate
Updating/upgrading an enviroment:
conda update -n pta
VENV
Most used command options
Creating a virtual environment using the current selected Python version:
python -m venv venv
Activating/deactiving the virtual environment:
source ./venv/bin/activate
deactivate
Updating/upgrading a virtual environment:
python -mvenv --upgrade venv
Removing an virtual enviroment (just delete the folder):
rm -rf ./venv