There are two major versions of the Python language, Python 2 which is the widely deployed legacy language and Python 3 which is the present and future of the language. Much Python code will work without modification between the last version of Python 2 (which is Python 2.7 (https://www.python.org/download/releases/2.7/)) and recent versions of Python 3, such as Python 3.5 (https://www.python.org/download/releases/3.5.1/). However, there are some key differences between the major versions, and in a strict sense the languages are incompatible. We'll be using Python 3.5 for this book, but we'll point out key differences with Python 2 as we go. It's also very likely that, this being a book on Python fundamentals, everything we present will apply to future versions of Python 3, so don't be afraid to try those as they become available.
Before we can start programming in Python we need to get hold of a Python environment. Python is a highly portable language and is available on all major operating systems. You will be able to work through this book on Windows, Mac or Linux, and the only major section where we diverge into platform specifics is coming right up — as we install Python 3. As we cover the three platforms, feel free to skip over the sections which aren’t relevant for you.
The following are the steps to be performed for Windows platform:
For Windows you need to visit the official Python website, and then head to the
Downloads
page by clicking the link on the left. For Windows you should choose one of the MSI installers depending on whether you're running on a 32- or 64-bit platform.Download and run the installer.
In the installer, decide whether you only want to install Python for yourself, or for all users of your machine.
Choose a location for the Python distribution. The default will be in
C:\Python35
in the root of theC:
drive. We don't recommended installing Python into Program Files because the virtualized file store used to isolate applications from each other in Windows Vista and later can interfere with easily installing third-party Python packages.On the
Customize Python
page of the wizard we recommend keeping the defaults, which use less than 40 MB of space.In addition to installing the Python runtime and standard library, the installer will register various file types, such as
*.py
files, with the Python interpreter.Once Python has been installed, you'll need to add Python to your system
PATH
environment variable. To do this, from the Control Panel chooseSystem and Security
, thenSystem
. Another way to get here easily is to hold down your Windows key and press the Break key on your keyboard. Using the task pane on the left chooseAdvanced System Settings
to open theAdvanced
tab of theSystem Properties
dialog. ClickEnvironment variables
to open the child dialog.If you have Administrator privileges you should be able to add the paths
C:\Python35
andC:\Python35\Scripts
to the semicolon separated list of entries associated with thePATH
system variable. If not, you should be able to create, or append to, aPATH
variable specific to your user containing the same value.Now open a new console window — either Powershell or cmd will work fine — and verify that you can run python from the command line:
> python Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
Welcome to Python!
The triple arrow prompt shows you that Python is waiting for your input.
At this point you might want to skip forward whilst we show how to install Python on Mac and Linux.
For macOS you need to visit the official Python website at http://python.org. Head to the
Download
page by clicking the link on the left. On theDownload
page, find the macOS installer matching your version of macOS and click the link to download it.A DMG Disk Image file downloads, which you open from your Downloads stack or from the Finder.
In the Finder window that opens you will see the file
Python.mpkg
multipackage installer file. Use the "secondary" clickaction
to open the context menu for that file. From that menu, selectOpen
.On some versions of macOS you will now be told that the file is from an unidentified developer. Press the
Open
button on this dialog to continue with the installation.You are now in the Python installer program. Follow the directions, clicking through the wizard.
There is no need to customize the install, and you should keep the standard settings. When it's available, click the
Install
button to install Python. You may be asked for your password to authorize the installation. Once the installation completes clickClose
to close the installer.Now that Python 3 is installed, open a terminal window and verify that you can run Python 3 from the command line:
> python Python 3.5.0 (default, Nov 3 2015, 13:17:02) [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
Welcome to Python!
The triple arrow prompt shows that Python is waiting for your input.
To install Python on Linux you will want to use your system's package manager. We'll show how to install Python on a recent version of Ubuntu, but the process is very similar on most other modern Linux distributions.
On Ubuntu, first start the Ubuntu Software Center. This can usually be run by clicking on it's icon in the launcher. Alternatively, you can run it from the dashboard by searching on Ubuntu Software Center and clicking the selection.
Once you're in the software center, enter the search term python 3.5 in the search bar in the upper right-hand corner and press return.
One of the results you'll get will say Python (v3.5) with Python Interpreter (v3.5) in smaller type beneath it. Select this entry and click the
Install
button that appears.You may need to enter your password to install the software at this point.
You should now see a progress indicator appear, which will disappear when installation is complete.
Open a terminal (using Ctrl+Alt+T) and verify that you can run Python 3.5 from the command line:
$ python3.5 Python 3.5.0+ (default, Oct 11 2015, 09:05:38) [GCC 5.2.1 20151010] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
Welcome to Python!
The triple arrow prompt shows you that Python is waiting for your input.