Installing optional packages with conda
Sooner or later, you will need a package that is not installed by default in your Anaconda distribution.
If you are using the Anaconda distribution, this recipe describes the preferred method of installing and uninstalling packages. Anaconda is built on top of conda
, a flexible and easy-to-use package manager.
Anaconda allows direct access to vast collections of add-on packages. For example, it makes it easy to install SciKits, which are SciPy packages developed by independent developers that are not part of the main distribution. A complete list of SciKits can be found at the following site: http://scikits.appspot.com/scikits.
Getting ready
This recipe assumes that you have a working installation of Anaconda. If you don't, follow the recipe for installing Anaconda for your operating system presented previously in this chapter.
How to do it...
As an example, lets install scikit-image
, a package for image processing available at the site http://scikit-image.org:
To install scikit-image
, first update conda
and Anaconda by running the following two commands in your Terminal:
conda update conda conda update anaconda
Now, scikit-image
can be installed by running the following command at the system prompt:
conda install scikit-image
After the installation is finished, use a text editor to create a file called skimage_test.py
containing the following code:
from skimage import data, io, filters image = data.coins() edges = filters.sobel(image) io.imshow(edges) io.show()
This code first constructs an image object from one of the sample images contained in scikit-image
, which in this example is a photograph of a collection of old coins. It then applies a Sobel filter, which emphasizes the edges of objects present in the image. Finally, it displays the image of the detected coin edges.
Save the file and run the script by entering the following statement at the command line:
python3 skimage_test.py
Once the script runs, an image of the coin edges is displayed. Close the image to exit the script.
Let's now suppose that you don't need scikit-image
any longer. It is easy to uninstall the package with conda
using the following command:
conda uninstall scikit-image
If, after uninstalling, you attempt to run the skimage_test.py
script, you will get an error message stating that Python can't import the skimage
library.
Finally, what if a package is not available in Anaconda? In this case, it is still possible to install the package with pip
, as outlined in the next section.
Note
An alternative to installing packages directly in the global Anaconda distribution is to create a virtual environment with conda
. Later in this chapter, we present several recipes for creating virtual environments with different requirements.