Book Image

The Python Apprentice

By : Robert Smallshire, Austin Bingham
Book Image

The Python Apprentice

By: Robert Smallshire, Austin Bingham

Overview of this book

Experienced programmers want to know how to enhance their craft and we want to help them start as apprentices with Python. We know that before mastering Python you need to learn the culture and the tools to become a productive member of any Python project. Our goal with this book is to give you a practical and thorough introduction to Python programming, providing you with the insight and technical craftsmanship you need to be a productive member of any Python project. Python is a big language, and it’s not our intention with this book to cover everything there is to know. We just want to make sure that you, as the developer, know the tools, basic idioms and of course the ins and outs of the language, the standard library and other modules to be able to jump into most projects.
Table of Contents (21 chapters)
Title Page
Credits
About the Authors
www.PacktPub.com
Customer Feedback
Preface
12
Afterword – Just the Beginning

Obtaining and installing Python 3


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.

Windows

The following are the steps to be performed for Windows platform:

  1. 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.

  2. Download and run the installer.

  3. In the installer, decide whether you only want to install Python for yourself, or for all users of your machine.

  4. Choose a location for the Python distribution. The default will be inC:\Python35 in the root of the C: 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.

  5. On the Customize Python page of the wizard we recommend keeping the defaults, which use less than 40 MB of space.

  6. 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.

  7. Once Python has been installed, you'll need to add Python to your system PATH environment variable. To do this, from the Control Panel choose System and Security, then System. 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 choose Advanced System Settings to open the Advanced tab of the System Properties dialog. Click Environment variables to open the child dialog.

  8. If you have Administrator privileges you should be able to add the pathsC:\Python35 and C:\Python35\Scripts to the semicolon separated list of entries associated with the PATH system variable. If not, you should be able to create, or append to, a PATH variable specific to your user containing the same value.

  9. 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.

macOS

  1. 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 the Download page, find the macOS installer matching your version of macOS and click the link to download it.

  2. A DMG Disk Image file downloads, which you open from your Downloads stack or from the Finder.

  3. In the Finder window that opens you will see the file Python.mpkg multipackage installer file. Use the "secondary" click action to open the context menu for that file. From that menu, select Open.

  4. 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.

  5. You are now in the Python installer program. Follow the directions, clicking through the wizard.

  6. 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 click Close to close the installer.

  7. 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.

Linux

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. You may need to enter your password to install the software at this point.

  6. You should now see a progress indicator appear, which will disappear when installation is complete.

  7. 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.