Building your Python scientific system from scratch

Table of contents

Introduction

As mentioned in the Overview document, there are some advantages to downloading and running WinPython. In particular, with that version you can easily move your projects among different computers. However, if you don't need to do that and you are running Windows, you may find it advantageous to download and install Python and then download and install the appropriate packages for the Python Scientific Computing Ecosystem.

Among other advantages, this will provide more control over where you store your Jupyter Notebook documents. It may also run faster than WinPython running on a USB device.

This document provides instructions for building your Python scientific system from scratch on a Windows machine.

Command-line operations

These instructions will require you to do some command-line operations. If you are unfamiliar with the command line, you may find it useful to review the following web page: Getting to Know the Command Line.

Several of the instructions will tell you to open a command-line window in a particular folder. Here is an easy way to do that if you are running Windows.

Use a text editor such as Notepad or Notepad++ to create a batch file named aacmd.bat in the folder of interest. (Make sure the file is NOT named aacmd.bat.txt, which can happen sometimes with Notepad.) Put the following two lines of text in the file:

echo off
cmd

When you double-click on that file in Windows Explorer, a command-line window should open in that folder similar to the one shown below. (If the double click simply causes the file to open, it probably has a hidden extension of .txt.)

missing image

You should only need to create the file once. Once you have created the file, you can copy it from folder to folder to open command-line windows in different folders.

Download and install Python

Your computer must be connected to the Internet for any of the installations described in this document to be possible.

Here are the steps for downloading and installing Python on a Windows machine. (Note that these instruction purposely do not install the program in the default folder.)

  1. Go to https://www.python.org/
  2. Hover on the Downloads tab. A popup window should appear.
  3. Click Python 3.x.y under Download for Windows (where 3.x.y represents the Python version) and save the file.
  4. Open C: on your computer.
  5. Create a folder named ProgramFiles in the root directory on C: (note there is no space in the name of the folder).
  6. Create a folder named Python3xy in the ProgramFiles folder where xy represents the version of Python that you will be installing.
  7. Find the file in your downloads folder, double click the file, and run it. The file name should be something similar to python-3.6.4.exe.
  8. When the installation dialog appears, check the box that reads something like "Add Python 3.6 to PATH".
  9. Select the link to "Customize installation".
  10. Make sure all optional features are selected and click "Next".
  11. Accept the pre-checked Advanced Options and "Browse" to the folder named Python3xy that you created earlier. (Note that this not the default installation folder.)
  12. Click "Install" and select "Yes" on the User Account Control window. (You must have administrator privileges to do the installation.)
  13. Click "Close" on the "Setup was successful" screen.

At this point you should be able to open the folder named Python3xy and see that Python has been installed.

Confirm your Python installation

The following operations can be used to confirm a successful installation.

Much of the material that follows was taken from the following web pages: Installing Packages and Installing Jupyter Notebook. Take a look at those pages for help if you experience problems.

Open a command-line window in some arbitrary folder and enter the following command:

python --version 

This should produce an output on the screen similar to the following:

Python 3.6.4

Ensure you can run pip from the command line by entering the following command at the command prompt:

pip --version

This should produce an output similar to the following:

pip 9.0.1 from c:\programfiles\python364\lib\site-packages (python 3.6)

Ensure pip, setuptools, and wheel are up to date by entering the following command at the command prompt:

python -m pip install --upgrade pip setuptools wheel

This will produce several lines of output text that should end with something like the following:

Successfully installed setuptools-38.4.0 wheel-0.30.0

Ensure that you have the latest version of pip by entering the following command at the command prompt:

pip install --upgrade pip

If pip is up to date, you should see something like the following:

Requirement already up-to-date: pip in c:\programfiles\python364\lib\site-packages

Otherwise you should see an output indicating that the latest version is being installed.

At this point you should be ready to use pip to install packages to supplement the basic Python system.

Install Jupyter Notebook

Before continuing, you should view the following three videos for an introduction to the Jupyter Notebook:

To install the Jupyter Notebook code, open a command-line window in an arbitrary folder and enter the following command at the command prompt. (The folder choice doesn't matter at this point because the Windows OS already knows where the required material is located.)

pip install jupyter

This will cause the pip program to download and install the latest version of the Jupyter Notebook from PyPI - the Python Package Index or perhaps some similar online archive.

This may take a while to complete while producing a large amount of output text on the command-line screen. Eventually the command prompt should return. If you open the Scripts folder in the Python3xy folder after the command prompt returns, you should see many files related to the Jupyter Notebook including a file named jupyter.exe.

Confirm your Jupyter Notebook installation

You can confirm your Jupyter Notebook installation by creating and executing a very simple Python program inside the Jupyter Notebook.

Open a command-line window in the folder where you plan to store your Jupyter Notebook documents and enter the following command at the command prompt:

jupyter notebook

You should see a lot of text appearing in that window and the jupyter notebook should open in your browser.

Open the New drop-down list in the upper right and select Python 3.

Enter the following code in the first cell:

print("Hello, World!")

Select the Kernel tab and then select Restart and Run All.

Select Restart and Run All Cells in the window that opens.

The following text should appear inside the cell immediately below the command indicating that you have successfully installed both Python 3 and the Jupyter Notebook.

Hello, World!

When you are satisfied with the results, do the following:

  1. Open the File menu in Jupyter Notebook and select "Close and Halt".
  2. Close the command-line window that you opened earlier to stop the application from running.

At this point you cannot run any code that requires libraries (such as matplotlib) that are not included in the standard Python distribution because you haven't installed any packages containing such libraries.

Install the matplotlib library

Open a command-line window in an arbitrary folder and enter the following command at the command prompt to download and install the latest version of the matplotlib library. This command will also download and install the latest version of the numpy library.

pip install matplotlib

After some time, the command prompt should return and matplotlib will have been installed. You should see a folder containing matplotlib at the following location:

C:\ProgramFiles\Python364\Lib\site-packages\matplotlib

You should also see a folder containing numpy at the following location:

C:\ProgramFiles\Python364\Lib\site-packages\numpy

Confirm your matplotlib, numpy, and Jupyter Notebook installations

You can test your installation with a script that requires the matplotlib and numpy libraries in addition to the standard Python libraries.

Confirm in a stand-alone mode

Copy the following code into a text file named proj01.py in an arbitrary folder:

import numpy as np
import matplotlib.pyplot as plt
import math

fig,axes = plt.subplots(2,2,
                        figsize=(7,5),
                        facecolor='0.75',
                        linewidth=10,
                        edgecolor='Red')

t = np.arange(0, 6.0, 0.05)
ax0,ax1,ax2,ax3 = axes.flatten()

ax0.plot(t, np.cos(1*np.pi*t))
ax0.set_ylabel('This is a ylabel')
ax0.set_xlabel('This is an xlabel')
ax0.set_title('A cosine')

ax1.plot(t, np.exp(-t) )
ax1.set_ylabel('This is a ylabel')
ax1.set_xlabel('This is an xlabel')
ax1.set_title('An exponential')

ax2.plot(t, np.exp(-t) * np.cos(3*np.pi*t))
ax2.set_ylabel('This is a ylabel')
ax2.set_xlabel('This is an xlabel')
ax2.set_title('A damped cosine')

ax3.plot(t, np.exp(-t) * np.cos(4*np.pi*t),label='Damped cosine')
ax3.plot(t, np.cos(4*np.pi*t),label='Cosine')
ax3.legend(loc='upper right',framealpha=0.3,facecolor='Green')
ax3.set_title('Subplot with a legend')
ax3.set_ylabel('This is a ylabel')
ax3.set_xlabel('This is an xlabel')

ax0.grid(True)
ax1.grid(True)
ax2.grid(True)
ax3.grid(True)

fig.suptitle('A figure title')
fig.tight_layout(pad=2)
plt.show()

Open a command-line window in that same folder and enter the following command:

python proj01.py

This should cause the image shown below to appear on your screen.

Missing image 

Confirm in Jupyter Notebook

You can use the same code to test Jupyter Notebook in conjunction with the newly installed libraries.

  1. Start Jupyter Notebook as before and create a new notebook for Python 3 using the drop-down list in the upper right.
  2. Copy the three import statements from above into the first cell.
  3. Click the + button to create a second cell .
  4. Copy the remaining code from above into the second cell.
  5. Pull down the Kernel menu and select "Restart and Run All".

The same image should appear immediately below the code except that it won't have the seven buttons along the bottom.

Install the pandas, seaborn, scipy and pillow libraries

While you are at it, you should probably go ahead and execute the following three commands at the command prompt to install the pandas, seaborn, scipy, and pillow libraries. You will need these libraries in this or the follow-on courses. You will learn about the first three later. The pillow library must be installed if you need to write output files in the jpeg format.

pip install pandas
pip install seaborn
pip install pillow

You may also discover other libraries that you will need to install as you progress through this coursework. If so, you should be able to use the knowledge gained here to do the installations.

Useful resources

If you have made it to this point, you have learned most of what you need to know to create your own Python development environment containing many of the packages that are freely available on the web. Some useful resources for you to study are listed below:

Housekeeping material

Author: Prof. Richard G. Baldwin
Affiliation: Professor of Computer Information Technology at Austin Community College in Austin, TX.
File: BuildingPythonFromScratch.htm
Revised: 04/24/18
Copyright 2018 Richard G. Baldwin

-end-