This is a sample section from Learning IPython for Interactive Computing and Data Visualization, second edition.
Although Python is an open-source, cross-platform language, installing it with the usual scientific packages used to be overly complicated. Fortunately, there is an all-in-one scientific Python distribution, Anaconda (by Continuum Analytics), that is free, cross-platform, and easy to install. Anaconda comes with Jupyter and all of the scientific packages we will use in this book. There are other distributions and installation options (like Canopy, WinPython, Python(x, y), and others), but for the purpose of this book we will use Anaconda throughout.
TIP (Running Jupyter in the cloud): You can also use Jupyter directly from your web browser, without installing anything on your local computer: go to http://try.jupyter.org. Note that the notebooks created there are not saved. Let's also mention a similar service, Wakari, by Continuum Analytics.
Anaconda comes with a package manager named conda, which lets you manage your Python distribution and install new packages.
TIP (Miniconda): Miniconda (http://conda.pydata.org/miniconda.html) is a light version of Anaconda that gives you the ability to only install the packages you need.
The first step is to download Anaconda from Continuum Analytics' website. This is actually not the easiest part since several versions are available. Three properties define a particular version:
INFO (Python 2 or Python 3?): Python 3 brought a few backward-incompatible changes. This is why many people are still using Python 2.7 at this time, even though Python 3 was released in 2008. We will use Python 3 in this book, and we recommend that newcomers learn Python 3. If you need to use legacy Python code that hasn't yet been updated to Python 3, you can use conda to temporarily switch to a Python 2 interpreter.
Once you have found the right link for your OS and Python 3 64-bit, you can download the package. You should then find it in your downloads
directory (depending on your OS and your browser's settings).
The Anaconda installer comes in different flavors depending on your OS:
.sh
script. Run it with a command like bash Anaconda3-2.3.0-Linux-x86_64.sh
(if necessary, replace the filename by the one you downloaded)..pkg
file that you can run with a double-click..exe
file that you can run with a double-click.Then, follow the instructions to install Anaconda on your computer. A few remarks:
INFO (Graphical launcher): Anaconda comes with a graphical launcher that you can use to start IPython, manage environments, and so on. You will find more details at http://docs.continuum.io/anaconda-launcher/
Before you get started with Anaconda, there are a few things you need to know:
You can skip this paragraph if you already know how to do these things.
A terminal is a command-line application that lets you interact with your computer by typing commands with the keyboard, instead of clicking on windows with the mouse. While most computer users only know Graphical User Interfaces, developers and scientists generally need to know how to use the command-line interface for advanced usage. To use the command-line interface, follow the instructions that are specific to your OS:
powershell
in the Run box, and press Enter. You will find more information about Powershell at https://blog.udemy.com/powershell-tutorial/. Alternatively, you can use the older Windows terminal by typing cmd
in the Run box.terminal
, and pressing Enter.In a terminal, use the cd /path/to/directory
command to move to a given directory. For example, cd ~
moves to your home directory, which is introduced in the next paragraph.
Your home directory is specific to your user account on your computer. It generally contains your applications' settings. It is often referred to as ~
. Depending on the OS, the location of the home directory is as follows:
C:\Users\YourName\
where YourName
is the name of your account/Users/YourName/
where YourName
is the name of your account/home/yourname/
where yourname
is the name of your accountFor example, the directory ~/anaconda3
refers to C:\Users\YourName\anaconda3\
on Windows and /home/yourname/anaconda3/
on Linux.
The PATH is a global variable (also called an environment variable) defined by your operating system with the list of directories where executable programs are located. If you type a command like python
in your terminal, you generally need to have a python
(or python.exe
on Windows) executable in one of the directories listed in the PATH. If that's not the case, an error may be raised.
You can manually add directories to your system's PATH:
rundll32.exe sysdm.cpl,EditEnvironmentVariables
, and press Enter. You can then edit the PATH variable and append ;C:\path\to\directory
if you want to add that directory. You will find more detailed instructions at http://www.computerhope.com/issues/ch000549.htm.~/.bash_profile
and add export PATH="$PATH:/path/to/directory"
at the end of the file.~/.bashrc
and add export PATH="$PATH:/path/to/directory"
at the end of the file.To test Anaconda once it has been installed, open a terminal and type python
. This opens a Python console, not to be confused with the OS terminal. The Python console is identified with a >>>
prompt string, whereas the OS terminal is identified with a $
(Linux/OS X) or >
(Windows) prompt string. These strings are displayed in the terminal, often preceded by your computer's name, your login, and the current directory (for example, yourname@computer:~$
on Linux or PS C:\Users\YourName>
on Windows). You can type commands after the prompt string. After typing python
, you should see something like the following:
$ python
Python 3.4.3 |Anaconda 2.3.0 (64-bit)| (default, Jun 4 2015, 15:29:08)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
What matters is that Anaconda
or Continuum Analytics
is mentioned here. Otherwise, typing python
might have launched your system's default Python, which is not the one you want to use in this book.
If you have this problem, you may need to add the path to the Anaconda executables to your PATH. For example, this path will be ~/anaconda3/bin
if you chose to install Anaconda in ~/anaconda3
. The bin
directory contains Anaconda executables including python
.
If you have any problem installing and testing Anaconda, you can ask for help on the mailing list (see the link at the end of this section).
Next, exit the Python prompt by typing exit()
and pressing Enter.
Anaconda lets you create different isolated Python environments. For example, you can have a Python 2 distribution for the rare cases where you need to temporarily switch to Python 2.
To create a new environment for Python 2, type the following command in an OS terminal:
$ conda create -n py2 anaconda python=2.7
This will create a new isolated environment named py2
based on the original Anaconda distribution, but with Python 2.7. You could also use the command conda env
: type conda env -h
to see the details.
You can now activate your py2
environment by typing the following command in a terminal:
activate py2
(note that you might have problems with Powershell, see https://github.com/conda/conda/issues/626, or use the old cmd
terminal)source activate py2
Now, you should see a (py2)
prefix in front of your terminal prompt. Typing python
in your terminal with the py2
environment activated will open a Python 2 interpreter.
Type deactivate
on Windows or source deactivate
on Linux/Mac OS X to deactivate the environment in the terminal.
Here is a list of common commands:
conda help
: display the list of conda commands.conda list
: list all packages installed in the current environment.conda info
: display system information.conda env list
: display the list of environments installed. The currently active one is marked by a star *
.conda install somepackage
: install a Python package (replace somepackage
by the name of the package you want to install).conda install somepackage=0.7
: install a specific version of a package.conda update somepackage
: update a Python package to the latest available version.conda update anaconda
: update all packages.conda update conda
: update conda itself.conda update --all
: update all packages.conda remove somepackage
: uninstall a Python package.conda remove -n myenv --all
: remove the environment named myenv
(replace this by the name of the environment you want to uninstall).conda clean -t
: remove the old tarballs that are left over after installation and updates.Some commands ask for confirmation (you need to press y
to confirm). You can also use the -y
option to avoid the confirmation prompt.
If conda install somepackage
fails, you can try pip install somepackage
instead. This will use the Python Package Index (PyPI) instead of Anaconda. Many scientific Anaconda packages are easier to install than the corresponding PyPI packages, because they are precompiled for your platform. However, many packages are available on PyPI but not on Anaconda.
Here are some references:
Here are a few references about Anaconda:
All of this book's code is available on GitHub as notebooks. We recommend that you download the notebooks and experiment with them as you're working through the book.
INFO (Git and GitHub): GitHub is a popular online service that hosts open source projects. It is based on the Git Distributed Version Control System (DVCS). Git keeps track of file changes and enables collaborative work on a given project. Learning a version control system like Git is highly recommended for all programmers. Not using a version control system when working with code or even text documents is now considered as bad practice. You will find several references at https://help.github.com/articles/good-resources-for-learning-git-and-github/. The IPython Cookbook also contains several recipes about Git and best interactive programming practices.
Here is how to download the book's notebooks:
git version
. You should see the version of git and not an error message.$ git clone https://github.com/ipython-books/minibook-2nd-code.git "$HOME/minibook"
This will download the very latest version of the code into a minibook
subdirectory in your home directory. You can also choose another directory.
From this directory, you can update to the latest version at any time by typing git pull
.
TIP (Notebooks on GitHub): Notebook documents stored on GitHub (with the file extension
.ipynb
) are automatically rendered on the GitHub website.