To work with Python on Windows, we must install Python. For Mac OS X and Linux, a version of Python is already present; we'll often want to add a newer version to the preinstalled Python.
There are two significantly different flavors of Python available:
This book is about Python 3.4. We won't cover Python 2.x at all. There are several visible differences. What's important is that Python 2.x is a bit of a mess under the hood. Python 3 reflects some fundamental improvements. The improvements came at the cost of a few areas where the two versions of the language had to be made incompatible.
The Python community is continuing to keep Python 2.x around. Doing this is a help to people who are stuck with old software. For the most part, developers are moving forward with Python 3 because it's a clear improvement.
Before we get started, it's important to know if Python is already installed. The general test to see if Python is already installed is to get an OS command prompt. For Windows, use Command Prompt; for Mac OS X or Linux, use the Terminal tool. We'll show Mac OS X prompts from the Mac OS X Terminal. It looks like this:
We've shown the OS prompt MacBookPro-SLott:~ slott$
. We entered the python3
command, which is typical for Linux and Mac OS X. In Windows, we'll often enter just python
. The response was three lines of introduction followed by the >>>
prompt. Enter exit
and hit return to get some useful advice on how to leave Python. This example showed Python 3.3, which is a little out of date. An upgrade isn't required.
Some kind of "command not found" error from the OS means we don't have any Python, so we'll need to do an install.
If we get a Python message that starts with something like "Python 2.7.6", we'll need to do an upgrade.
The next section covers Windows installations. After that, we'll look at Mac OS X and then we will see Linux upgrades. In some cases, we may develop software on Windows desktop computers, but the ultimate destination is a large, centralized Linux server. The Python files can be the same between these two environments, so having Python on multiple platforms won't be very complex or confusing.
Installing Python on Windows
Python runs on many versions of Windows. There are some older, less-widely-used versions of Windows without an actively supported version of Python. For example, Windows 2000 is not supported.
The general procedure for installing Python is quite simple. We'll download an installer and do some preparation. Then we'll start the installer. Once that's finished, we'll be up and running.
To find the installer, start here:
https://www.python.org/downloads/
The web server should detect your OS and provide a big button with some variation of "Download Python 3.4.x" on it. Click on this button to start the download.
To look at the choices available, the https://www.python.org/downloads/windows/ path provides all of the actively-supported versions of Python. This will show a long list of older versions. There are two installers available:
- The Windows x86 MSI installer
- The Windows x86-64 MSI installer
If we have a very old computer, we might need the 32-bit version. Most modern computers will have a 64-bit CPU. When in doubt, 64-bit is the assumption to make.
Double-click the .msi
file to start running the installer. This starts with a question about installing Python for yourself or for all users. If you have appropriate privileges, the all users option is appropriate. On a shared computer, without appropriate privileges, you'll have to install it for yourself only.
The second page will ask for an installation directory. Be careful about the path that you choose for the installation, and avoid spaces in filenames.
Tip
Do not install Python into directories with spaces in their names. Avoid names such as "Program Files" and "My Documents". The space may cause problems that are difficult to diagnose.
Install Python into a simple directory with a short, space-free name like C:\python34
.
Spaces in filenames is not a general problem, but it is awkward when first starting. There are many ways to cope with spaces in filenames. When learning a new programming language, however, it's important to minimize the awkward problems so that we can focus on the important topics.
The next page will also show a menu of components that can be installed; it's easiest to request everything. There's no compelling reason to turn off any of the optional components. We'll be looking at the IDLE development tool, which requires the Tcl/Tk package, so it's important to be sure that this is part of the installation.
In many cases, the final option on this list updates the system environment variables to include Python on the PATH
variable. This isn't enabled by default, but it can be helpful if you're going to write BAT files in Windows.
In addition to the basic Python interpreter, the Windows help installer is very helpful. This is a separate download and requires a quick installation. After we've installed this, we can use the F1 key to bring up all of the Python documentation.
Once Python is installed, the Using the Read-Evaluate-Print Loop (REPL) section will show how to start interacting with Python.
Considering some alternatives
We'll focus on a particular implementation of Python called CPython. The distinction we're making here is that Python—the abstract language—can be processed by a variety of concrete Python runtimes or implementations. The CPython implementation is written in portable C and can be recompiled for many operating systems. This implementation tends to be extremely fast.
For Windows developers, there's an alternative implementation called Iron Python. This is tightly integrated with the Windows .NET development environment. It has the advantage of working with Visual Studio. It has the disadvantage of being based on the Python 2.7 language.
Another choice Windows users have is to use Python Tools for Visual Studio (PTVS). This will allow you to use Python 3.4 from within Visual Studio. For developers who are used to Visual Studio, this might prove helpful.
Other Python implementations include Jython, Stackless Python, and PyPy. These alternatives are available for all operating systems, so we'll address these in the Looking at other Python interpreters section later.
Upgrading to Python 3.4 in Mac OS X
Python runs on all versions of Mac OS X. It turns out that Mac OS X relies on Python. However, it relies on Python 2.7, so we'll need to add Python 3.4.
The general procedure for installing Python on Mac OS X is quite simple. We'll download a disk image (.dmg
) installer and do some preparation. Then we'll start the installer that's in the disk image. Once that's finished, we'll be up and running.
To find an installer, start here:
https://www.python.org/downloads/
The web server should detect your OS and provide a big button with some variation of "Download Python 3.4.x" on it. Click on this and download the .dmg
file.
To look at the choices available, the https://www.python.org/downloads/mac-osx/ path provides all of the actively-supported versions of Python for Mac OS X. This will show alternatives for older versions of Python.
When the .dmg
device becomes available after the download, double-click on the .mpkg
installer file to start running the installer.
Clicking on Continue will step through the Read Me, License, Destination Select, and Installation Type windows. There's a Customize button that allows us to turn options on and off. We won't need to do this—the default installation is ideal.
We'll need to provide the username and password of a user who's authorized to administer this computer. This will not remove the existing Python that Mac OS X uses. It will add another version of Python. This means that we'll have at least two copies of Python. We'll focus on using Python 3, ignoring the built-in Python, which is Python 2.
To use Python 3, we have to enter python3
at the OS prompt in the Terminal window. If we have both Python 3.3 and Python 3.4, we can enter the even more specific python3.4
at the command prompt to specify which version of Python 3 we're using. Generally, the python3
command will be the latest-and-greatest version of Python 3. The python
command—unadorned with a version number—will be the Python 2.x that Mac OS X requires.
Adding the Tkinter package
Python relies on a library named Tkinter to provide support for writing programs with a GUI. This package relies on Tcl/Tk. The details can be found here:
https://www.python.org/download/mac/tcltk/
The summary of this is that we need to install version 8.5.17 or newer. See https://www.python.org/download/mac/tcltk/#activetcl-8-5-17-0. This will provide a graphic environment that Python will use. We must install Tcl/Tk in order for the tkinter
package to work.
After we download the .dmg
file and open the .pkg
file, we'll see this window:
We'll be looking at the IDLE development tool, which requires tkinter
. Consequently, this additional installation is essential.
We can avoid this extra download if we avoid using tkinter
. Some developers prefer to use the Active State Komodo editor as their development tool; this does not require Tcl/Tk. Also, there are numerous add-on GUI frameworks that don't require tkinter
.
Upgrading to Python 3.4 in Linux
For Linux, the latest Python may already be installed. When we enter python3
, we may see that we already have a useful version available. In this case, we're ready to roll. In some cases, the OS will only have an older Python (perhaps older than 2.7) installed. In this case, we'll need to upgrade.
For Linux distributions, there are two paths for upgrading Python:
- Installing prebuilt packages: Many distributions have appropriate packages already available. We can use a package manager (such as
yum
or RPM
) to locate and install the necessary Python package. In some cases, there will be additional dependencies, leading to a cascade of downloads and installs. Since Python 3.4 is relatively new, there may not be very many prebuilt packages for your particular Linux distribution. Details are available at https://docs.python.org/3/using/unix.html#on-linux. - Building from source: Most Linux distributions include the GNU C compiler. We can download the Python source, configure the build script, and use
make
and make install
to build Python. This may require upgrading some Linux libraries to assure that your Linux installation has the required support for Python 3.4. The installation steps are summarized as ./configure
, make
, and sudo make altinstall
. Details are available at https://docs.python.org/3/using/unix.html#building-python.
When we use altinstall
, we'll end up with two Pythons installed. We'll have an older Python, which we can run using the python
command. The python3
command will, generally, be linked to the latest version of Python 3. If we need to be explicit, we can use the python3.4
command to select a specific version.
As with the Mac OS X installation, adding the Python tkinter
package is important. Sometimes, this is separate from the basic package. This may lead to upgrading Tcl/Tk, which may lead to some more downloads and installs. At other times, the Linux distribution has an up-to-date Tcl/Tk environment and nothing more needs to be done.
We can avoid the extra Tcl/Tk download if we avoid using tkinter
. As mentioned earlier, many developers prefer to use the Active State Komodo editor as their development tool; this does not require tkinter
. Also, there are numerous GUI frameworks that aren't based on tkinter
.