Search icon CANCEL
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
SciPy Recipes

You're reading from   SciPy Recipes A cookbook with over 110 proven recipes for performing mathematical and scientific computations

Arrow left icon
Product type Paperback
Published in Dec 2017
Publisher Packt
ISBN-13 9781788291460
Length 386 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
V Kishore Ayyadevara V Kishore Ayyadevara
Author Profile Icon V Kishore Ayyadevara
V Kishore Ayyadevara
Ruben Oliva Ramos Ruben Oliva Ramos
Author Profile Icon Ruben Oliva Ramos
Ruben Oliva Ramos
Luiz Felipe Martins Luiz Felipe Martins
Author Profile Icon Luiz Felipe Martins
Luiz Felipe Martins
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Getting to Know the Tools FREE CHAPTER 2. Getting Started with NumPy 3. Using Matplotlib to Create Graphs 4. Data Wrangling with pandas 5. Matrices and Linear Algebra 6. Solving Equations and Optimization 7. Constants and Special Functions 8. Calculus, Interpolation, and Differential Equations 9. Statistics and Probability 10. Advanced Computations with SciPy

Installing SciPy from source on Linux

Since Linux is an umbrella name for a number of distinct operating system configurations, there is no binary distribution that fits all possible Linux flavors.

All modern distributions of Linux come with Python pre-installed. In this recipe, we describe a setup procedure for a local Python 3 installation from source that works on two of the most popular Linux distributions, Ubuntu and Debian. The installation will be located in the user's home directory, and will not conflict with any pre-installed version of Python that exists in the system.

How to do it...

The installation procedure is broken down in the following stages:

  • Installing Python 3
  • Installing the SciPy Stack

Installing Python 3

Start by opening a Terminal window and running the following commands, one at a time. You will be required to enter the administrative password for your system which, on a personal computer, is usually your own login password:

sudo apt-get install build-essential
sudo apt-get install sqlite3 libsqlite3-dev
sudo apt-get install bzip2 libbz2-dev
sudo apt-get install libreadline-dev libncurses5-dev
sudo apt-get install libssl-dev tcl-dev tk-dev python3-tk
sudo apt-get install zlib1g-dev liblzma-dev

Next, download a source distribution of Python from the site https://python.org.

Make a note of the file name, which will look like the following: Python-3.x.x.tar.xz, where x.x is a pair of numbers that specify the build for this distribution. You should download the highest version available, which should be above 3.6.0.

Now, go to the directory where the downloaded file was saved and run the following command, replacing x.x with the corresponding build number of your file:

tar xvf Python-3.x.x.tar.xz

To build the distribution, execute the following commands, again replacing x.x with the correct build number:

cd Python-3.x.x
./configure --prefix=$HOME/python3
make

The build process will take a while to finish, depending on the configuration and speed of your system.

The following step is optional. If you want to run the battery of tests included with the source distribution, run the following command from the Terminal window:

make test

Depending on how fast your computer is, this may take a long time. At the end of the tests, a report of the build process will be generated. Do not worry if you get a few messages about skipped tests.

Next, install the code in its target directory, by running the following command:

make install

We now need to adjust the PATH environment variable. Start by making a backup copy of your shell configuration file by running the following commands:

cd
cp .bashrc .bashrc.python.bak

Start your text editor, open the .bashrc file, and add the following line at the end of the file:

export PATH=$HOME/python3/bin:$PATH

Save the file, close the editor and, back at the Terminal window, run the following command:

source ~/.bashrc

To test the installation, start Python from the command line by running the following command:

python3

If the installation is correct, Python will start and print information about the interpreter, as follows:

Python 3.x.x (default, Apr  4 2017, 09:40:21) 
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

Check that the correct version of Python is being started by verifying that the version number 3.x.x coincides with that of the distribution you downloaded.

If you get an error when trying to start Python, or the wrong version of Python starts, the PATH variable was probably not set correctly in the .bash_rc file. You can check the current value of PATH by entering the echo $PATH command. If the path is not correct, edit .bashrc as indicated previously.

Exit Python by running the following command at the >>> Python prompt:

quit()

As a final test, run the following statement from the command line to check that Python's package manager was installed:

pip3 --version

This will print information about the version of pip3 that you are running. If no error message is issued, the setup was completed correctly and you can proceed to installing SciPy.

Installing the SciPy stack

Open a Terminal window and enter each of the following commands in succession:

pip3 install --user numpy scipy matplotlib
pip3 install --user ipython jupyter PyQt5
pip3 install --user pandas sympy nose

Make a backup copy of the .bashrc file and open it with a text editor. For example, to use nano, run the following commands in a Terminal window:

cd
cp .bashrc .bashrc.scipy.bak
nano .bashrc

Add the following line at the end of .bashrc:

export PATH="$HOME/.local/bin:$PATH"

Save the file, close the editor, and run the following command from the Terminal window:

source .bashrc

Let's now test the installation. Start Python by running the following command in a Terminal window:

python3

Execute the following lines at the >>> Python prompt. There will be no output and, as long as there are no errors, the installation is correct:

import numpy
import scipy
import matplotlib
import IPython
import pandas
import sympy
import nose

Exit Python by running the following at the prompt:

quit()

Back at the Terminal prompt, run the following command:

ipython

This will start IPython, an alternative Python shell with many added features, and print a startup message. You can check that the Python shell is running the expected version of Python from the top line of the startup message. Exit IPython by entering, at the prompt, the following command:

quit()

Back at the Command Prompt, run the following from the command line:

jupyter notebook

If all is correct, the Jupyter Notebook will start on your browser after a while. You have now concluded the installation of Python 3 and the SciPy stack in Linux.

You have been reading a chapter from
SciPy Recipes
Published in: Dec 2017
Publisher: Packt
ISBN-13: 9781788291460
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 €18.99/month. Cancel anytime