Preamble
A venv is an isolated standalone python distribution with a specific version of modules. This is useful when one needs to run different python versions in a single system. Various commands can create a venv: venv, virtualenv, conda… We are going to use Anaconda to set up various python virtual environments on our system.
References: - Virtualenv - Python documentation on venv
Python package system
The Python Package Index (PyPI) is a repository of software for the python programming language. PyPI helps you find and install software developed and shared by the python community.
The pip program allows you to install most standard packages:
$ pip --version
$ pip install numpyAlso it is possible to install a package from a file:
$ pip install -r requirements.txtwhere requirements.txt is a file containing the list of packages to install.
In particular such a file can be generated by the following command:
$ pip freeze > requirements.txtor to output only the packages installed into your virtual env:
$ pip freeze --all > requirements.txtAn altarnative could be using the pipreqs package:
$ pipreqs /path/to/projectReferences: - Pypi - Installing Packages
Anaconda
Anaconda is a package manager, an environment manager coming with a python/R data science distribution, and a large collection of open-source packages. It is cross-platform and is a very popular choice in the data scientist community. Nevertheless, it suffers from a main drawback: it is heavy. Moreover, it comes with its own package manager conda which allows you to install a python module (like pip) and other programs.
On the Linux box provided by the FdS, there is a terminal with the $PATH environment variable already configured (/net/apps/bin/init_anaconda3). You may launch it via the Graphical User Interface.
You can see also the mamba project https://github.com/mamba-org/mamba.
Creating an environment
Use the terminal or an Anaconda Prompt for the following steps:
To create an environment:
$ conda create --name myenvReplace
myenvwith the environment name.When conda asks you to proceed, type
y:proceed ([y]/n)?By default, environments are installed into the
envssub-directory in yourcondadirectory. Seeconda create --helpfor information on specifying a different path. This creates themyenvenvironment inenvs/. This environment uses the same version ofpythonthat you are currently using because you did not specify a version.To create an environment with a specific version of
python:$ conda create -n myenv python=3.9To create an environment with a specific package:
$ conda create -n myenv scipyor:
$ conda create -n myenv python $ conda install -n myenv scipyTo create an environment with a specific version of a package:
$ conda create -n myenv scipy=0.15.0or
$ conda create -n myenv python $ conda install -n myenv scipy=0.15.0To create an environment with a specific version of
pythonand multiple packages:$ conda create -n myenv python=3.6 scipy=0.15.0 astroid babel
Switch environment
To switch to an environment, it must be “activated” (in git we would have said “to checkout”). Activation entails two primary functions: adding entries to PATH for the environment and running any activation scripts that the environment may contain. These activation scripts are how packages can set arbitrary environment variables that may be necessary for their operation. You can also use the config API to set environment variables. To activate an environment:
$ conda activate myenvChange myenv with the name of your environment.
Save and export an environment
References: Building identical conda environments
Removing an environment and cleaning
Anaconda is particularly greedy in terms of disk usage. It can be a good practice to remove an unused environment
$ conda env remove -n myenvTo remove all cache and package run
$ conda clean --all