In this chapter, we will review how to set up the Python 2.7 32-bit edition on Windows, Mac, and Ubuntu Linux. We will walk through the Python interpreter and build a few Hello-World-style Python applications to ensure our code is working properly. This will be covered primarily in the Windows section of the chapter, but it will be reiterated in other OS sections.
We will also review how to install and use easy_install and pip, which are package managers that are commonly used in Python development. We will also review how to install lxml, which is a popular xml parser and writer that we will need in later chapters.
If you're fairly new to Python, you might have heard that Python doesn't have the right build tools to run on Windows or that Python is optimized for Unix-based systems such as Mac OS X and Linux variations. In part, this is true; most libraries, including ones that are covered in this book, work better and are easier to install if you are on an operating system that isn't Windows.
I want to spend a little extra time in this section in case you, the reader, want to use Windows as your development OS while working through this book. Firstly, I want to cover why Windows is known to have issues with Python developers. Typically, it's not the language that causes issues, and nor the lack of editors. In fact, Windows has even more high-quality editors for Python, including Visual Studio with Python Tools, and more text editor options such as Notepad++.
The real problem that plagues developers is library compatibility, specifically, Python libraries that reference C-based code to achieve results that are not possible using the Python language directly. Unlike Mac OS X or Linux variations, Windows does not include a C compiler as a part of the OS. Typically, when a Python library author mentions Windows's "lack of build tools", this usually refers to Windows not including a C compiler.
Another issue is the command prompt; it's typical in Python development to install libraries and assets using the terminal or using the command prompt in Windows commands. The two common commands to install libraries are
pip. If you're not familiar, easy_install is a command-line based package manager for Python. It uses Python eggs, (a renamed
.zip file specific to easy_install) to bundle the scripts and required files for a library. The easy_install package manager is also an older package manager and has been in the Python tool belt for ages. It's typical to find older Python libraries using easy_install. The following screenshot shows you the PyPI website:
The other command, called pip, is also known as Python Package Index (PyPi). Whereas easy_install has been community driven, PyPi is the official package manager of the Python Software Foundation, the group that is in charge of updates and taking care of the Python language. The site also hosts third-party packages.
The following screenshot shows you the Python website:
Newer libraries are usually created using pip for two reasons. One, pip has more features than easy_install and two, pip libraries are searchable on Python's official package site repository at https://pypi.python.org/pypi.
Let's start with installing Python on your Windows machine. For this book, I'll be using Windows 8.1, though this workflow should be fine if you're running Windows 7 or Windows Vista. First, open up your browser of choice and navigate to http://www.python.org/.
On the home page, you should see a download link as shown in the preceding screenshot. For Windows, we are looking for Python Version 2.7+ (the 32-bit Version). Go ahead and click on that link and you'll be taken to the download page:
On the download page, you'll want to download the Windows x86 MSI installer. We want the 32-bit installer rather than the 64-bit installer. This will ensure optimal compatibility with packages in upcoming chapters. The following screenshot shows you the general installation window for Python on Windows (shown here with a 64-bit version of Python for demo purposes):
Once you've downloaded the installer, double-click on the installer to run it. Follow the wizard and leave the defaults alone, particularly the path where Python is installed as shown in the preceding screenshot. Let the installer work through the installation and reboot your system.
After rebooting your system, if you're in Windows 8 on the desktop tile, right-click on the Start screen icon and click on System. Then, click on Advanced system settings (if you're in Windows 7 or Vista, you can find this by navigating to Control Panel | All Control Panel Items | System), as shown in the following screenshot:
Once you've done that, you'll want to click on Environment Variables, as shown in the preceding screenshot, and look for Path under System variables. These variables allow the command prompt to know what programs it has access to anywhere in your system. We have to edit the Path as shown in the following screenshot, select Path, and click on Edit:
With the Edit menu visible, type
C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts\; (including the semicolon at the front to differentiate paths) at the end of the variable value. Click on OK on both windows to save the changes and reboot your PC again.
Now, let's test your Python installation! Open up your command prompt, and type
python in lowercase and press Enter. Assuming the installer worked properly, you should see the command prompt path cursor location change to precede
>>>, as shown in the following screenshot:
You are now in the Python interpreter; here, you can run simple one line scripts such as the following command:
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
The next line will output
Hello Reader!, showing your Python script print to the console, with the following
>>> waiting for your next command. You can also process commands such as:
2 + 2, hit Enter, and you will see
4 on the next line.
Let's try to save a variable to the prompt; type the following command on the next line:
authorsName = 'Chad'
Press Enter. Then, type the following command and press Enter again:
The output is shown in the next screenshot:
Now, your command prompt will look like the preceding screenshot. Notice on the resulting line that
Chad is the output for the
authorsName Python variable. This means that you've installed the Python compiler correctly! We've confirmed that Python works on Windows by testing the function object, the math object, and the variable objects.
With that tested, you can return to the standard command prompt from the Python compiler by exiting the compiler. Simply type
exit(0) to exit the Python instance.
Now that we have reviewed the command line on Windows, we need to know a few other things before we start writing code. Let's start with where Python and any libraries are installed on your machine. Open Windows Explorer and navigate to
C:\Python27, as shown in the following screenshot:
Python27 directory, you can see the
python.exe file; this is the application that our Path in System variables looks for to run Python scripts and commands. This folder also contains other libraries that are to required be run by Python, including libraries downloaded from easy_install or pip.
You can find the third-party libraries by navigating to
C:\Python27\Lib\site-packages. Any libraries and any third-party dependencies downloaded through pip or easy_install will be installed in this directory by default.
Next, let's pull down a few libraries we will need for this book. Python 2.7 on Windows pip and easy_install are included with Python's Windows Installer by default. First, we will need the
lxml library. Now, on Windows, the
lxml library is a very popular C-based XML parser and writer library for Python libraries and is notoriously incompatible with Windows systems due to its C-based implementation. Let's install the
lxml library before pulling packages that might depend on this, staring with
lxml, as shown in the following screenshot:
lxml does come in both pip and easy_install flavors; however, since it's C-based, we require the Windows installer found at https://pypi.python.org/pypi/lxml/3.3.3. Grab the
lxml-3.3.3.win32-py2.7.exe file or a newer Version 2.7 library and run the installer. Once it's installed, we can confirm the installation by navigating to the
site-packages directory and checking whether any new folder called
lxml has been created. When installed, the
site-packages directory should look like the following screenshot:
lxml is installed, we will set up easy_install and pip. First, let's download easy_install and install it. The steps are as follows:
Navigate your browser of choice to https://pypi.python.org/pypi/setuptools.
Then, download the
Now, open your command prompt again with administrator privileges, then type the following command, and press Enter:
Next, type the following command and press Enter:
When you're finished, your command prompt should look like the following screenshot:
If you're successful, your command prompt should look something like the following screenshot:
With that done, let's test pip! We want to try to install a library called
BeautifulSoup. It's a common Python library for scrapping HTML content. We won't be using
BeautifulSoup but we need to test the pip installation, and
BeautifulSoup is a good library that works with most installations. To install
BeautifulSoup in your console while still it's open and the path is still pointing to your
C:\Python27 directory, type the following command:
pip install beautifulsoup
You'll see a message at the end, as shown in the following screenshot:
We have now installed the necessary libraries and frameworks that are required to build Python scripts, so let's pick a code editor. For first-time (and even veteran Python) developers, I recommend an IDE as an editor of choice over a plain text editor. This is mainly for two reasons. One, an IDE typically includes code hinting of some kind to give the developer an idea of what Python packages are available or even installed on the developer's system. Two, most good IDEs include Python-specific code-documentation templates and helpers that help write large code bases.
One of the more popular IDEs is Eclipse with PyDev; it's free and is a very good starter IDE for Python. We will cover Eclipse in more depth in the next sections for other platforms, but if you intend to use Eclipse on Windows, be sure to install the latest Java runtime and JDK installers for your version of Windows. Read ahead to learn more about Eclipse with PyDev.
If you come from a .NET background or prefer Visual Studio in general, check out Python Tools for Visual Studio. This allows you to run Python code in a Visual Studio project and be able to keep Python code in Team Foundation Server (Microsoft's source control system). The following screenshot shows the Python Tools for Visual Studio website:
To install Python Tools for Visual Studio, grab the installer from http://pytools.codeplex.com/ (shown in the preceding screenshot). Also, if you don't own Visual Studio, the Python Tools can be installed on Visual Studio for Desktop or Visual Studio for Web, which are free downloads by Microsoft. You can download the express editions at http://www.visualstudio.com/products/visual-studio-express-vs.
The following screenshot shows the IronPython website:
You might also notice IronPython at http://ironpython.net/. IronPython is Python optimized for Windows with access to the .NET libraries, which means that you can access .NET properties with Python, such as
For this book, we will use CPython, (typically referred to as normal Python libraries with nothing added). Keep in mind that some libraries written in Python might or might not work in IronPython, depending on its dependencies.
Let's build a quick Python script in Visual Studio with Python Tools before moving on to OS X. In the following screenshot, you will see the New Project window. Notice the options for normal (CPython) called Python Application as well as other project types such as Django and IronPython. We want Python Application for this book.
Once you've installed the Python Tools for Visual Studio, open Visual Studio, create a new project under Python, choose Python Application, and name it
Pyname, as shown in the preceding screenshot. Right-click on the
Pyname project and click on Properties. Set your interpreter to Python 2.7 and click on Save in the toolbar, as shown in the following screenshot:
Now, take a look at Solution Explorer and expand your Python Environments | Python 32-bit 2.7. You'll be able to see that the third-party libraries we've installed are now visible in Visual Studio, as shown in the following screenshot (shown here with a 64-bit version of Python for demo purposes):
authorName = ('Chad') print(authorName)
Now hit Start and you'll see the command prompt automatically launch with
Chad printed on the screen. Success; you just wrote Python in Visual Studio!
In this section, we covered the following topics:
Installing Python in Windows
Installing easy_install and pip
lxml, a common Python library
From here on, Python gets easier to install. If you're on a Mac, many consider Python the best to be run on due to the inclusion of build tools and compilers. Before we install Python, it's important to know that OS X includes Python with the OS. One issue, though, is that it doesn't include everything that the base installer does. Also, OS X locks out some command-line features that are common in Unix systems that can cause issues for some Python modules and libraries.
In this section, we will review the Eclipse IDE on OS X with PyDev 3.0 and review using easy_install and pip using OSX. First, install Python by going to https://www.python.org/ and downloading the 2.7.7 (or higher) 32-bit
Once it's installed, open the terminal and test easy_install. Since easy_install is included by default, we can use easy_install to install pip. Type the following command in your console:
sudo easy_install pip
sudo in the console will prompt you for your administrator password. Depending on your version, your output might mention that you have it already installed; that's okay, this means that your package managers for Python are ready. Now, try testing the Python compiler. In the terminal, type
python and press the return key.
This should look something like the following screenshot; notice the version number in the interpreter to confirm which version is active.
Now, let's test the interpreter; try typing the following command:
The output should be
Hello Reader!. Now, let's try our
authorName variable script (shown in the following screenshot) to confirm that variables in Python are being saved. Type both lines shown in the following screenshot, and it should look like the following example. If so, congrats; Python and its base libraries are installed!
With Python installed, we can now focus on an editor. There are several Python IDEs out for OS X, Aptana, and Pycharm, but the one we will use (and the one that tends to be popular among Python developers) is PyDev for Eclipse. At the time of writing this, Eclipse Kepler (4.3.2) has released, as has PyDev Version 3.0. Both require Java 7 and JDK 7 or higher installed for PyDev to work properly. So, before installing Eclipse and PyDev, install the latest JRE and JDK by visiting the following links:
Once you've installed both Java runtime and JDK, reboot your Mac and navigate your browser of choice to http://www.eclipse.org and download the Eclipse Kepler (4.3.2) classic edition (32-bit or 64-bit, depending on your system). The classic edition is Eclipse by itself, with no plugins or project types included. Once this is done, extract the Eclipse
.zip file to a folder on your desktop and open the Eclipse application. On launching Eclipse the first time, set your workspace path and click on OK. Eclipse will reboot and relaunch Eclipse. Also on Safari, we might get a Plug-in blocked for this site message. To continue, the user must click on Trust. This is a security measure to confirm that the user wants to install an external package or a plugin. Click on Trust to install.
Also, you'll need the JDK and Java 7 runtime or higher, since it's required for the current version of PyDev. The process for the OS X installation should be the same.
Now, with Eclipse loaded, navigate to Help | Eclipse Marketplace. Then, in the Search field, type
Pydev. You should see something like the following screenshot:
Click on Install Now and follow the prompts, including approving the certificate by selecting the I agree radio button for PyDev and clicking on Finish, followed by quitting Eclipse. Once Eclipse is restarted, you can change the IDE for Python development by navigating to Window | Open Perspective | Other | Pydev and clicking on OK.
Next, let's configure our interpreter so that when we run our Python code, the IDE can process our run requests. The easiest way is in Eclipse.
Navigate to Window | Preferences | PyDev | Interpreter (Python/Jython/IronPython).
Then, run Auto Config by clicking on Auto Config in the interpreter window. Your paths will be set up automatically. If you run into an issue, you can set it manually and point to the executable by navigating to Library | Frameworks | Python.Framework | Versions | 2.7 | bin | python2.7-32.
Now, let's write some code with Eclipse. With Eclipse restarted, navigate to File | New | Pydev Project.
Create a project with the
Pyname name, as shown in the following screenshot. Next, create a
pyname.py file in the project explorer on the right.
Linux-based operating systems such as Ubuntu are Python's home in many ways. The Ubuntu marketplace is written in Python, and many Linux apps usually have a Python code base. Ubuntu features many of the same terminal commands OS X uses, if not the same commands. For Ubuntu, we will focus on Ubuntu 13.10. If you're using a derivative of Ubuntu, Lubuntu, Xubuntu, or Linux Mint, there are a few points that need to be kept in mind.
Most of these commands should be the same, with a few minor differences depending on your setup. Reach out to your search engine of choice if you run into issues loading each software component. The same can be said for Debian or Red Hat-based Linux distros.
Like OS X, you'll need the Java 7 runtime or higher and JDK 7 runtime or higher. Ubuntu does not include these in its included package manager, but you can install them via the command line.
The good news is that Python 2.7 is included with Ubuntu 13.10, so we will not need to install Python. We can even test this by opening the terminal and typing
python in the Bash prompt, as shown in the following screenshot:
The easy_install and pip Python package managers are commonly used to install and update our packages. Next, grab both easy_install and pip, and install both of these tools using the following commands:
sudo apt-get install python-setuptools sudo apt-get install python-pip
Remember, in Ubuntu the
sudo command will ask for a password before installing the
python-pip. Now, if this is successful, the terminal should return the following message, as shown in the following screenshot:
Next, before installing Eclipse with PyDev, let's download Java 7 and JDK. To do this, we will add it to our system package repository and install Java. Open the terminal and type the following commands:
sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java7-installer
Be sure to use the keyboard keys on the licensing agreement in the terminal, as shown in the following screenshot:
Now, let's install Eclipse and PyDev. Navigate to http://www.eclipse.org/ using your browser of choice, and download Eclipse Classic. Unpack the download and open Eclipse, then select a workspace path, and click on OK.
At the time of writing this, there is a bug for Eclipse's menus being reskinned in Ubuntu. If you're experiencing this issue, check online for a command-line fix, as this can vary between updates. Ubuntu 14.04 LTS is planned in order to have this bug resolved in the release.
Once this is done, open Eclipse Marketplace by navigating to Help | Eclipse Marketplace and search for PyDev. Install the plugin and agree to the certificate, then reboot Eclipse.
Assuming everything is installed properly, you'll see PyDev in the Preferences section of Eclipse.
One final note on Ubuntu: since Ubuntu has its own package manager,
apt-get, we can install packages for Python using it as well, for example, using
sudo apt-get install python-lxml
At this point, you should be all set for the Python development. Try recreating our
authorName script from our OS X and Windows sections.