09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 1 ■ INTRODUCTION TO CLIENT/SERVER NETWORKING<br />

Careful <strong>Python</strong> programmers do not suffer from this situation any longer. Many <strong>of</strong> us install only<br />

one <strong>Python</strong> package system-wide: virtualenv. Once virtualenv is installed, you have the power to create<br />

any number <strong>of</strong> small, self-contained “virtual <strong>Python</strong> environments” where packages can be installed,<br />

un-installed, and experimented with without contaminating your system-wide <strong>Python</strong>. When a<br />

particular project or experiment is over, you simply remove its virtual environment directory, and your<br />

system is clean.<br />

In this case, we want to create a virtual environment in which to test the googlemaps package. If you<br />

have never installed virtualenv on your system before, visit this URL to download and install it:<br />

http://pypi.python.org/pypi/virtualenv<br />

Once you have virtualenv installed, you can create a new environment like this (on Windows, the<br />

directory containing the <strong>Python</strong> binary in the virtual environment will be named “Scripts” instead):<br />

$ virtualenv --no-site-packages gmapenv<br />

$ cd gmapenv<br />

$ ls<br />

bin/ include/ lib/<br />

$ . bin/activate<br />

$ python -c 'import googlemaps'<br />

Traceback (most recent call last):<br />

File "", line 1, in <br />

ImportError: No module named googlemaps<br />

As you can see, the googlemaps package is not yet available! To install it, use the pip command that is<br />

inside your virtualenv and that is now on your path thanks to the activate command that you ran:<br />

$ pip install googlemaps<br />

Downloading/unpacking googlemaps<br />

Downloading googlemaps-1.0.2.tar.gz (60Kb): 60Kb downloaded<br />

Running setup.py egg_info for package googlemaps<br />

Installing collected packages: googlemaps<br />

Running setup.py install for googlemaps<br />

Successfully installed googlemaps<br />

Cleaning up...<br />

The python binary inside the virtualenv will now have the googlemaps package available:<br />

$ python -c 'import googlemaps'<br />

Now that you have the googlemaps package installed, you should be able to run the simple program<br />

named search1.py.<br />

Listing 1–1. Fetching a Longitude and Latitude<br />

#!/usr/bin/env python<br />

# <strong>Foundations</strong> <strong>of</strong> <strong>Python</strong> <strong>Network</strong> <strong>Programming</strong> - Chapter 1 - search1.py<br />

from googlemaps import GoogleMaps<br />

address = '207 N. Defiance St, Archbold, OH'<br />

print GoogleMaps().address_to_latlng(address)<br />

Running it at the command line, you should see a result like this:<br />

$ python search1.py<br />

(41.5228242, -84.3063479)<br />

3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!