Setting up Python on Mac OS X

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

Setting up Python on Mac OS X

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

Remember, using 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.

Setting up Python on Mac OS X

Now, let's test the interpreter; try typing the following command:

print('Hello Reader!')

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!

Setting up Python on Mac OS X

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:

Setting up Python on Mac OS X

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.

Setting up Python on Mac OS X

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.

Setting up Python on Mac OS X

Finally, type the following code as shown in the following screenshot and click on Run. If successful, you will see Chad in the output window.

Setting up Python on Mac OS X

In this section, we covered how to install Python on OS X, installing pip using easy_install, working with the terminal, and setting up Eclipse with PyDev.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.223.190