A Virtual Environment is a copy of an existing Python installation with the option to inherit existing packages, and install new ones, without affecting the main Python installation in your Operating System (OS). Two of the main advantages of Virtual Environments are:
Conda is package manager for Python that can create virtualized development environments and handle your project dependencies without affecting your main Python install.
Miniconda, is a smaller alternative to Anaconda, which only includes Conda (the package manager) and its bare minimum dependencies.
Anaconda is a distribution of Conda with a multitude of commonly used Python packages, such as: "numpy," scipy," "ipython notebook," etc.
Having installed Miniconda, you can easily upgrade to Anaconda by typing in your terminal:
conda install anaconda
conda -V
In a terminal, type:
conda update conda
To create a new Virtual Environment from scratch:
cd my-project
myvirtenv
, using Python version
3.5
, and with no default packages, type:
conda create --no-default-packages -n myvirtenv python=3.5Replace
myvirtenv
for your own custom Virtual Environment name, and 3.5
for the Python version that you would like to use in your project. Remove the
flag --no-default-packages
to install Conda default packages when creating your environment.
Ensure that an environment file, for example environment.yml
, exists in your current directory, and then type in your terminal:
conda env create -f environment.yml
Type into your terminal:
conda info --envs
If you are using a Mac or Linux distribution, in order to activate or switch to your Virtual Environment
(myvirtenv
), type in your terminal:
source activate myvirtenv
In a Windows machine, use instead:
activate myvirtenv
Your active Virtual Environment will be displayed between parenthesis or brackets at the at the beginning of your command prompt:
(myvirtenv) $
To install additional packages, for example scipy
, in the Virtual Environment that you just created myvirtenv
without affecting your main Python install, type:
conda install -n myvirtenv scipy
If your target environment is currently active (displayed between parenthesis or brackets at the at the beginning of your command prompt) you can just type:
conda install scipy
You can even install other package managers, such as the well-known pip
, and use them to install more packages, for example flask
, within your currently activated Virtual Environment:
conda install pip
pip install flask
Note that you can also use other pip
subcommands.
If the environment myvirtenv
is not activated:
conda list -n myvirtenv
However, if your the environment is active, you can simply type:
conda list
Do you see the pattern? ;)
To export your environment packages to an
environment.yml
file, you need to
activate the environment to export, and then type:
conda env export > environment.yml
Note that if an environment.yml file already exists in your current directory, it will be overwritten.
Example of a Conda environment file, for a basic machine learning project named myvirtenv
:
To end the session in your currently activated Virtual Environment, type:
source deactivate
In a Windows machine, just use:
deactivate
To delete, for example myvirtenv
, with all its dependencies, you would type:
conda remove -n myvirtenv -all
Comments and Feedback