Creating a conda environment with a different version of a package
We often need to run code that is not compatible with a particular version of a package. In this situation, it is useful to use conda
to find out what version of a package is currently installed. For instance, to get information about which version of the numpy
package is being used at the moment, we can execute the command shown as follows at the system prompt:
conda info numpy
On my computer, this outputs the following information:
numpy 1.12.1 py36_nomkl_0 file name : numpy-1.12.1-py36_nomkl_0.tar.bz2 name : numpy version : 1.12.1
Getting ready
This recipe assumes that you have a working installation of Anaconda. If you don't, follow the recipe for installing Anaconda on your operating system presented previously in this chapter.
How to do it...
Let's now suppose that we have a legacy package that depends on an earlier version of NumPy. For example, let's assume we need version 1.7 of NumPy. We can check what versions of NumPy are available in the Anaconda repository with the following command:
conda search numpy
Running this command will produce a long list, in which we find the following information:
... 1.7.1 py33_0 defaults ... 1.7.1 py33_2 defaults ...
conda
shows several available packages of NumPy, some of them compatible with Python 3. Let's now create an environment that has the older version of NumPy installed. Start by creating the new environment with the following command:
conda create --name numpy17 python=3 numpy=1.7
Notice that we specify the required versions for both Python and NumPy. conda
will then display the following information about the environment to be created:
The following NEW packages will be INSTALLED: numpy: 1.7.1-py33_2 openssl: 1.0.1k-1 pip: 8.0.3-py33_0 python: 3.3.5-3 readline: 6.2-2 ...
Notice that conda
will downgrade the version of Python in the new environment. conda
packages are carefully designed to prevent incompatibilities. Go ahead and accept the changes to start the installation.
When the installation is complete, activate the new package. On Linux and macOS, we do this with with the following command:
source activate numpy17
On Windows, use the following command to activate the environment:
activate numpy17
After the environment is activated, we can install any packages that require version 1.7 of NumPy.
When we have finished working on the project, we need to deactivate the environment. On Linux and macOS, we use the following command:
source deactivate numpy17
On Windows, the command to deactivate an environment is as follows:
deactivate numpy17