Virtual Python Environment
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 numpy
References: - 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 myenv
Replace
myenv
with the environment name.When conda asks you to proceed, type
y
:proceed ([y]/n)?
By default, environments are installed into the
envs
sub-directory in yourconda
directory. Seeconda create --help
for information on specifying a different path. This creates themyenv
environment inenvs/
. This environment uses the same version ofpython
that 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.9
To create an environment with a specific package:
$ conda create -n myenv scipy
or:
$ conda create -n myenv python $ conda install -n myenv scipy
To create an environment with a specific version of a package:
$ conda create -n myenv scipy=0.15.0
or
$ conda create -n myenv python $ conda install -n myenv scipy=0.15.0
To create an environment with a specific version of
python
and 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 myenv
Change 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 myenv
To remove all cache and package run
$ conda clean --all