\(\def\dt{\Delta t}\)
\(\newcommand{\transpose}[0]{^\mathrm{T}}\)
\(\newcommand{\half}[0]{\tfrac{1}{2}}\)
\(\newcommand{\Half}[0]{\frac{1}{2}}\)
\(\newcommand{\norm}[1]{\left\lVert#1\right\rVert}\)
\(\newcommand\given[1][]{\:#1\vert\:}\)
UP | HOME

Anaconda Installation and Management

Table of Contents

1 Introduction

Anaconda is a very common complete Python system – it includes it's own version of the Python interpreter and it's own package management system. The supposed benefit is that it allows people to easily install a huge collection of Python packages in an isolated environment. On a Linux or Mac machine, the effect is that you have fewer conflicts with Python packages installed by your system's package manager or pip (Python's package manager). On Windows, the biggest advantage is that without Anaconda, there is a lot of work required to get all Python-related things setup and working correctly.

I personally really don't like Anaconda. I'm on a Linux system that already has easy ways of installing any Python package, and Python has built-in tools for providing isolated environments (virtualenv). However, there is no doubt Anaconda is popular – when you start internet searching how to solve Python problems, you are likely to encounter people discussing Anaconda. Even here at Northwestern, many courses in the EECS department advocate the use of Anaconda. So I figured it is worth spending a few minutes installing Anaconda, and talking about how it interacts with your system.

2 Notes on Installation

As of today, when you go to the Anaconda download page you download a large binary shell script executable. To run the installer, you need to give it execute permissions and then run it:

cd ~/Downloads
chmod a+x Anaconda2-5.2.0-Linux-x86_64.sh
./Anaconda2-5.2.0-Linux-x86_64.sh

First you will have to accept a license agreement by typing yes. Then it will ask for your desired install location. Note that it defaults to installing in your home directory you are really only installing for your user. I typically accept the default install directory.

Next it asks about modifying your PATH:

Do you wish the installer to prepend the Anaconda3 install location to PATH in your home/<user>.bashrc ? [yes|no]

You should answer no. If you say yes it modifies your PYTHONPATH and PATH environment variables in a way that is often difficult to deal with. Basically Anaconda packages and Python interpreters become the default and these versions can interact badly with other tools that depend on Python (a big example of this is ROS). If you choose to use Anaconda (or are required to do so for a class), I recommend a way of actively controlling whether Anaconda is active in a given terminal or not. That way you can explicitly activate Anaconda when it's needed by adding it's binary directory to your current terminal's PATH; it's probably also a good idea to unset your PYTHONPATH so as not to interfere with the Anaconda distribution. Here's a first draft of two functions that could be added to your ~/.bashrc to do just that. Tweak as needed:

  function anaconda_activate() {
    CONDA_LOCATION=$HOME/anaconda2/bin
    if [ -n "$PATH" ];
    then
        echo "Storing current PATH"
        export NON_CONDA_PATH=$PATH
        echo "Prepending ${CONDA_LOCATION} to PATH"
        export PATH=$CONDA_LOCATION:$PATH
    fi
    if [ -n "$PYTHONPATH" ];
    then
        echo "Storing current PYTHONPATH"
        export NON_CONDA_PYTHONPATH=$PYTHONPATH
        echo "Unsetting PYTHONPATH"
        unset PYTHONPATH
    fi
    echo "####################"
    echo "Current environment is:"
    echo "PATH = ${PATH}"
    echo "PYTHONPATH = ${PYTHONPATH}"
    echo "####################"
    echo ""
}


function anaconda_deactivate() {
    if [ -n "$NON_CONDA_PATH" ];
    then
        echo "Restoring original PATH"
        export PATH=$NON_CONDA_PATH
    fi
    if [ -n "$NON_CONDA_PYTHONPATH" ];
    then
        echo "Restoring original PYTHONPATH"
        export PYTHONPATH=$NON_CONDA_PYTHONPATH
    fi
    echo "####################"
    echo "Current environment is:"
    echo "PATH = ${PATH}"
    echo "PYTHONPATH = ${PYTHONPATH}"
    echo "####################"
    echo ""
}

Note that Anaconda has something very analogous to Python's virtualenv that allows you to have multiple sets of Python packages installed within a single Anaconda installation. These are called Anaconda Environments. These can also be used to completely activate or deactivate Anaconda in a given terminal. Personally, I prefer the transparency and ease-of-understanding of the functions above. More importantly, AFAIK, Anaconda's tools don't manage your PYTHONPATH environment variable – so you could still run into issues with Anaconda Python interpreters finding Python packages outside of an Anaconda Environment through this variable. Even when I'm using Anaconda Environments, I typically first use the function above to "start" Anaconda in my terminal, and then use Anaconda's tools for activating/deactivating particular environments, and finally use the function above to completely "stop" Anaconda in that terminal. See below for an example of using Anaconda's tools to first activate Anaconda in general, then activate a particular environment, and then deactivate that environment, and finally deactivate Anaconda:

jarvis@test2018:~$ source ~/anaconda2/bin/activate
(base) jarvis@test2018:~$ which python
/home/jarvis/anaconda2/bin/python
(base) jarvis@test2018:~$ source activate myenv
(myenv) jarvis@test2018:~$ which python
/home/jarvis/anaconda2/envs/myenv/bin/python
(myenv) jarvis@test2018:~$ source deactivate
(base) jarvis@test2018:~$ which python
/home/jarvis/anaconda2/bin/python
(base) jarvis@test2018:~$ source deactivate
jarvis@test2018:~$ which python
/usr/bin/python

3 Working with Anaconda on Linux

I've helped many students debug problems where the problem basically came down to one of two things:

  1. They are using their built-in Python interpreter but somehow have Anaconda packages on their Python's search path
  2. They are using Anaconda's Python interpreter but Anaconda is somehow finding system Python packages

If you use the functions above, you should mostly avoid these issues, but if you do run into problems, here are a few tips and tricks that you can use for debugging these issues.

3.1 How to figure out which Python is being invoked?

This is very easy with the which Linux command. Typing which COMMAND will expand to the full path of COMMAND. For example:

jarvis@test2018:~⟫ which python
/usr/bin/python

If the result of which is the wrong executable you almost certainly have an issue with your PATH environment variable.

3.2 Where is Python searching?

As discussed in our Python intro Python searches in several places for modules to import. After opening a Python interpreter you can see the full list of places that your Python is searching by looking at the sys.path variable. See example below:

import sys
for p in sys.path:
    print p

Executing the above script:

jarvis@test2018:~⟫ python show_sys_path.py
/home/jarvis
/home/jarvis/hackathon_ws/devel/lib/python2.7/dist-packages
/opt/ros/melodic/lib/python2.7/dist-packages
/home/jarvis/.local/lib/python2.7/site-packages
/usr/local/lib
/usr/lib/python2.7/config
/usr/local/lib/python2.7/site-packages
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/local/lib/python2.7/dist-packages/trep-1.0.3-py2.7-linux-x86_64.egg
/usr/local/lib/python2.7/dist-packages/modern_robotics-1.0.0-py2.7.egg
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages/gtk-2.0
/usr/lib/python2.7/dist-packages/wx-3.0-gtk3

If the entries in sys.path are not what you expect, the first step is to check the contents of your PYTHONPATH environment variable (e.g. in a terminal run echo $PYTHONPATH). If the PYTHONPATH environment variable is what you expect, but there are still strange paths in sys.path, you could try looking for Python path configuration files (try running locate .pth and scanning the output for files with the pth extension somewhere on sys.path. These config files allow users and package maintainers to specify arbitrary paths to add to the sys.path search path. I have occasionally seen random Python packages include a pth file that resulted in very unintended consequences. If you find some pth files that might be modifying your path, feel free to look at the code and see what paths they are adding. If you end up deciding that one of these pth files is causing an issue then you need to get it off of sys.path by either editing/removing the file or possibly modifying PYTHONPATH to avoid including the offending pth file.

3.3 How to figure out what file is imported by Python?

It is easy to tell exactly which file Python finds by using a the .__file__ method. After importing a module you can call module.__file__= to figure out what file was actually imported. Example:

jarvis@togwotee:~⟫ ipython
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
Type "copyright", "credits" or "license" for more information.

IPython 5.8.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import numpy

In [2]: print numpy.__file__
/usr/local/lib/python2.7/dist-packages/numpy/__init__.pyc
Creative Commons License
ME 495: Embedded Systems in Robotics by Jarvis Schultz is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.