16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

208 CHAPTER 10 ■ BATTERIES INCLUDED<br />

As you may remember, the list of direc<strong>to</strong>ries (the so-called search path) can be found in<br />

the path variable in the sys module:<br />

>>> import sys, pprint<br />

>>> pprint.pprint(sys.path)<br />

['C:\\<strong>Python</strong>24\\Lib\\idlelib',<br />

'C:\\WINDOWS\\system32\\python24.zip',<br />

'C:\\<strong>Python</strong>24',<br />

'C:\\<strong>Python</strong>24\\DLLs',<br />

'C:\\<strong>Python</strong>24\\lib',<br />

'C:\\<strong>Python</strong>24\\lib\\plat-win',<br />

'C:\\<strong>Python</strong>24\\lib\\lib-tk',<br />

'C:\\<strong>Python</strong>24\\lib\\site-packages']<br />

■Tip If you have a data structure that is <strong>to</strong>o big <strong>to</strong> fit on one line, you can use the pprint function from the<br />

pprint module instead of the normal print statement. pprint is a pretty-printing function, which makes<br />

a more intelligent prin<strong>to</strong>ut.<br />

This is a relatively standard path for a <strong>Python</strong> 2.4 installation on Windows. You may not get<br />

the exact same result. The point is that each of these strings provides a place <strong>to</strong> put modules if<br />

you want your interpreter <strong>to</strong> find them. Even though all these will work, the site-packages<br />

direc<strong>to</strong>ry is the best choice because it’s meant for this sort of thing. Look through your sys.path<br />

and find your site-packages direc<strong>to</strong>ry, and save the module from Listing 10-4 in it, but give it<br />

another name, such as another_hello.py. Then try the following:<br />

>>> import another_hello<br />

>>> another_hello.hello()<br />

Hello, world!<br />

As long as your module is located in a place like site-packages, all your programs will be<br />

able <strong>to</strong> import it.<br />

Solution 2: Telling the Interpreter Where <strong>to</strong> Look<br />

Solution 1 might not be the right for you for a number of reasons:<br />

• You don’t want <strong>to</strong> clutter the <strong>Python</strong> interpreter’s direc<strong>to</strong>ries with your own modules.<br />

• You don’t have permission <strong>to</strong> save files in the <strong>Python</strong> interpreter’s direc<strong>to</strong>ries.<br />

• You would like <strong>to</strong> keep your modules somewhere else.<br />

The bot<strong>to</strong>m line is that if you place your modules somewhere else, you have <strong>to</strong> tell the<br />

interpreter where <strong>to</strong> look. As you saw earlier, one way of doing this is <strong>to</strong> edit sys.path, but that<br />

is not a common way <strong>to</strong> do it. The standard method is <strong>to</strong> include your module direc<strong>to</strong>ry (or<br />

direc<strong>to</strong>ries) in the environment variable PYTHONPATH.

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

Saved successfully!

Ooh no, something went wrong!