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 a binary distribution on macOS

macOS currently ships with version 2.7 of Python pre-installed. In this recipe, we will show you how to install Python 3 on the Mac without making changes to the original Python distribution. The easiest way to achieve this is to use Homebrew, a package manager for macOS.

How to do it...

The full installation instructions are broken down into the following stages:

  • Installing the Xcode command-line tools
  • Installing Homewbrew
  • Installing Python 3
  • Installing the SciPy stack

Installing the Xcode command-line tools

Xcode is the free development environment for macOS distributed by Apple. If you already have Xcode installed on your computer, you can skip this step. If you don't, open a Terminal window and run the following command:

xcode-select --install

If you get an error message, then the command-line tools are already installed and you can go to the next step. Otherwise, a window will pop up asking for confirmation. Press the Install button and, when prompted, accept the license agreement.

To check that the command-line tools were correctly installed, run the following command in the Terminal:

gcc -v

This command prints information about the gcc compiler present in your computer, which will be similar to the output shown as follows:

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.38)
Target: x86_64-apple-darwin16.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

If you get no error message, the command-line tools are properly installed.

Installing Homebrew

Homebrew is a package manager for macOS that makes it easier to install and remove software packages without interfering with system software that ships with the computer. It installs package files to the /usr/local directory and makes no changes to system folders. Although it is possible to install Python on the Mac from the source, using Homebrew considerably simplifies the setup process.

To install Homebrew, open a Terminal window and run the following command. Please note that the whole command should be typed in a single Terminal line:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Follow the on-screen instructions and confirm that you want to install Homebrew. Enter the administrative password for your computer if prompted. On a personal computer, this is usually the same as your login password.

To check that Homebrew was successfully installed, run the following command:

brew -v

This command outputs information about the current Homebrew installation, which looks like the following example:

Homebrew 1.1.13
Homebrew/homebrew-core (git revision c80e; last commit 2017-04-26)

If you get a similar message and no errors, Homebrew is properly installed.

Installing Python 3

Once Homebrew is set up, install Python 3 by running the following command from a Terminal window:

brew install python3

The installation process will start and may take a few minutes. When it is finished, run the following from the command line:

python3

If the installation is correct, this will print information about the Python interpreter, shown 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.
>>>

You can check that you are indeed running the Python distribution that you installed by checking the version number, indicated by 3.x.x in the preceding sample. You can now exit Python by running the following command at the >>> Python prompt:

quit()

We are going to use the pip3 Python package manager to install the SciPy stack. To check that pip3 was correctly installed, run the following statement from the command line:

pip3 --version

This will print to the Terminal information about the currently installed version of pip, as shown in the following example:

pip 9.0.1 from /home/fmartins/python3/lib/python3.x/site-packages (python 3.x)

Verify that you are indeed running the version of pip3 associated with your installation of Python by checking the version number, indicated by 3.x in the preceding sample output. If no error message is issued, the setup was completed correctly, and you can proceed to install SciPy.

Installing the SciPy stack

To install the SciPy stack, execute each of the following commands on a Terminal window:

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

We now need to adjust the PATH variable in the .bash_profile file. Notice that you might not have a .bash_profile yet. If you do, it is important to make a backup copy of it by running the following commands at the command line:

cd
cp .bash_profile .bash_profile.bak

If you get a message stating that .bash_profile does not exits, do not worry. We will create one now.

Start your text editor and open the .bash_profile file. For example, to use nano, a simple text editor included with macOS, run the following in a Terminal window:

cd
nano .bash_profile

This will create .bash_profile if it still does not exist. Add the following line at the end of file, where you need to replace 3.x by the version of Python you have installed:

export PATH="$HOME/Library/Python/3.x/bin:$PATH"

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

source .bash_profile

This completes the installation of a basic SciPy stack. To test the setup, start Python 3 in the Terminal window by running the following command:

python3

To check that all packages we need were installed, execute the following lines at the >>> Python prompt. Notice that 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

You can now exit the Python shell by running the following statement at the prompt:

quit()

Let's now check that IPython is accessible from the command line. Run the following line from the Terminal window:

ipython

This will start IPython, an alternative Python shell with many added features that is required to run Jupyter. A message similar to the following will be printed:

Python 3.x.x (default, Apr  4 2017, 09:40:21) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.0.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]:
If you get an error when trying to start IPython, the PATH variable was probably not set correctly in the .bash_profile file. You can check the current value of the PATH variable by entering the echo $PATH command. If the path is not correct, edit .bash_profile as indicated previously to correct the error.

Exit Python by entering, the following command in the IPython prompt:

quit()

Let's now test if we can run the Jupyter Notebook. Run the following command from the Terminal window:

jupyter notebook

If all is correct, you will see a series of startup messages in the Terminal, and the Jupyter Notebook will eventually start on your browser. If you get an error message, you probably have an incorrectly configured PATH variable. Check the preceding tip box for instructions on how to fix it.

This finishes the installation of Python and the SciPy stack in macOS. Please proceed to the Setting up a virtual environment section.

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 $19.99/month. Cancel anytime