Using conda environments

Most of the time, using venv is a fine choice. However, when writing high-performance code, it often happens that some high-performance libraries also require non-Python software to be installed. This typically involves additional setting up of compilers and high-performance native libraries (in C, C++, or Fortran) to which Python packages link. As venv and pip are designed to deal with Python packages only, this scenario is poorly supported by these tools.

The conda package manager was created specifically to handle such cases. Creating a virtual environment with conda can be done using the conda create command. The command takes a -n argument (-n stands for --name) that specifies an identifier for the newly created environment and the packages we intend to install. If we wish to create an environment that uses python version 3.5 and the latest version of NumPy, we use the following command:

$ conda create -n myenv Python=3.5 numpy

Conda will take care of fetching the relative packages from their repositories and placing them in an isolated Python installation. To enable the virtual environment, you can use the source activate command:

$ source activate myenv

After executing this command, the default Python interpreter will be switched to the version we specified earlier. You can easily verify the location of your Python executable using the which command, which returns the full path of the executable:

(myenv) $ which python
/home/gabriele/anaconda/envs/myenv/bin/python

At this point, you are free to add, remove, and modify packages in the virtual environment without affecting the global Python installation. Further packages can be installed using the conda install <package name> command or through pip.

The beauty of virtual environments is that you can install or compile any software you want in a well-isolated fashion. This means that if, for some reason, your environment gets corrupted, you can scratch it and start from zero.

To remove the myenv environment, you first need to deactivate it, and then use the conda env remove command, as follows:

(myenv) $ source deactivate
$ conda env remove -n myenv

What if a package is not available on the standard conda repositories? One option is to look whether it is available in the conda-forge community channel. To search for a package in conda-forge, you can add the -c option (which stands for --channel) to the conda search command:

$ conda search -c conda-forge scipy

The command will list a series of packages and versions available that match the scipy query string. Another option is to search for the package in the public channels hosted on Anaconda Cloud. The command-line client for Anaconda Cloud can be downloaded by installing the anaconda-client package:

$ conda install anaconda-client

Once the client is installed, you can use the anaconda command-line client to search for packages. In the following example, we demonstrate how to look for the chemview package:

$ anaconda search chemview 
Using Anaconda API: https://api.anaconda.org

Run 'anaconda show <USER/PACKAGE>' to get more details:
Packages:
Name | Version | Package Types | Platforms
------------------------- | ------ | --------------- | ---------------
cjs14/chemview | 0.3 | conda | linux-64, win-64, osx-64
: WebGL Molecular Viewer for IPython notebook.
gabrielelanaro/chemview | 0.7 | conda | linux-64, osx-64
: WebGL Molecular Viewer for IPython notebook.

Installation can then be easily performed by specifying the appropriate channel with the -c option:

$ conda install -c gabrielelanaro chemlab
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.137.67