Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning IPython for Interactive Computing and Data Visualization, Second Edition

You're reading from   Learning IPython for Interactive Computing and Data Visualization, Second Edition Get started with Python for data analysis and numerical computing in the Jupyter notebook

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher
ISBN-13 9781783986989
Length 200 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Cyrille Rossant Cyrille Rossant
Author Profile Icon Cyrille Rossant
Cyrille Rossant
Arrow right icon
View More author details
Toc

Installing Python with Anaconda

Although Python is an open-source, cross-platform language, installing it with the usual scientific packages used to be overly complicated. Fortunately, there is now 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 (https://wakari.io), 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.

Downloading Anaconda

The first step is to download Anaconda from Continuum Analytics' website (http://continuum.io/downloads). This is actually not the easiest part since several versions are available. Three properties define a particular version:

  • The operating system (OS): Linux, Mac OS X, or Windows. This will depend on the computer you want to install Python on.
  • 32-bit or 64-bit: You want the 64-bit version, unless you're on an old or low-end computer. The 64-bit version will allow you to manipulate large datasets.
  • The version of Python: 2.7, or 3.4 (or later). In this book, we will use Python 3.4. You can also use Python 3.5 (released in September 2015) which introduces many features, including a new @ operator for matrix multiplication. However, it is easy to temporarily switch to a Python 2.7 environment with Anaconda if necessary (see the next section).

    Note

    Python 3 brought a few backward-incompatible changes over Python 2 (also known as Legacy Python). 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).

Installing Anaconda

The Anaconda installer comes in different flavors depending on your OS, as follows:

  • Linux: The Linux installer is a bash .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).
  • Mac: The Mac graphical installer is a .pkg file that you can run with a double-click.
  • Windows: The Windows graphical installer is an .exe file that you can run with a double-click.

Then, follow the instructions to install Anaconda on your computer. Here are a few remarks:

  • You don't need administrator rights to install Anaconda. In most cases, you can choose to install it in your personal user account.
  • Choose to put Anaconda in your system path, so that Anaconda's Python is the system default.

Note

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...

Before you get started with Anaconda, there are a few things you need to know:

  • Opening a terminal
  • Finding your home directory
  • Manipulating your system path

You can skip this section if you already know how to do these things.

Opening a terminal

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:

  • On Windows, you can use Powershell. Press the Windows + R keys, type 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.
  • On OS X, you can open the Terminal application, for example by pressing Cmd + Space, typing terminal, and pressing Enter.
  • On Linux, you can open the Terminal from your application manager.

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 section.

Finding your home directory

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:

  • On Windows, its location is C:\Users\YourName\ where YourName is the name of your account.
  • On OS X, its location is /Users/YourName/ where YourName is the name of your account.
  • On Linux, its location is generally /home/yourname/ where yourname is the name of your account.

For example, the directory ~/anaconda3 refers to C:\Users\YourName\anaconda3\ on Windows and /home/yourname/anaconda3/ on Linux.

Manipulating your system path

The system 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 system path. If that's not the case, an error may be raised.

You can manually add directories to your system path as follows:

  • On Windows, press the Windows + R keys, type 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.
  • On OS X, edit or create the file ~/.bash_profile and add export PATH="$PATH:/path/to/directory" at the end of the file.
  • On Linux, edit or create the file ~/.bashrc and add export PATH="$PATH:/path/to/directory" at the end of the file.

Testing your installation

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 system 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 in the References section under the Installing Python with Anaconda section of this chapter).

Next, exit the Python prompt by typing exit() and pressing Enter.

Managing environments

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:

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/OS X to deactivate the environment in the terminal.

Common conda commands

Here is a list of common commands:

  • conda help: Displays the list of conda commands.
  • conda list: Lists all packages installed in the current environment.
  • conda info: Displays system information.
  • conda env list: Displays the list of environments installed. The currently active one is marked by a star *.
  • conda install somepackage: Installs a Python package (replace somepackage by the name of the package you want to install).
  • conda install somepackage=0.7: Installs a specific version of a package.
  • conda update somepackage: Updates a Python package to the latest available version.
  • conda update anaconda: Updates all packages.
  • conda update conda: Updates conda itself.
  • conda update --all: Updates all packages.
  • conda remove somepackage: Uninstalls a Python package.
  • conda remove -n myenv --all: Removes the environment named myenv (replace this by the name of the environment you want to uninstall).
  • conda clean -t: Removes 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:

References

Here are a few references about Anaconda:

Downloading the notebooks

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.

Note

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:

  • Install git: http://git-scm.com/downloads.
  • Check your git installation: Open a new OS terminal and type git version. You should see the version of git and not an error message.
  • Type the following command (this is a single line):
    $ 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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime