04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

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

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

CHAPTER 10 ■ BATTERIES INCLUDED 209<br />

ENVIRONMENT VARIABLES<br />

Environment variables are not part of the Python interpreter—they’re part of your operating system. Basically,<br />

they are like Python variables, but they are set outside the Python interpreter. To find out how to set them you<br />

should consult your system documentation, but here are a few pointers:<br />

In UNIX, you will probably set environment variables in some shell file that is executed every time you log<br />

in. If you use a shell such as bash, the file is .bashrc, found in your home directory. Add the following to that<br />

file to add the directory ~/python to your PYTHONPATH:<br />

export PYTHONPATH=$PYTHONPATH:~/python<br />

Note that multiple directories are separated by colons. Other shells may have a different syntax for this<br />

so you should consult the relevant documentation.<br />

In Windows, you may be able to edit environment variables from your Control Panel (in reasonably<br />

advanced versions of Windows, such as Windows XP, 2000, and NT; on older versions such as Windows 98,<br />

this does not work, and you would have to edit your autoexec.bat file instead, as covered in the next paragraph).<br />

From the Start menu, select Start ➤ Settings ➤ Control Panel. In the Control Panel, double-click the<br />

System icon. In the dialog box that opens, select the Advanced tab and click the Environment Variables button. That<br />

brings up another dialog box with two tables: one with your user variables and one with system variables. You<br />

are interested in the user variables. If you see PYTHONPATH there already, select it and click Edit, and edit it.<br />

Otherwise, click New and use PYTHONPATH as the name; enter your directory as the value. Note that multiple<br />

directories are separated by semicolons.<br />

If the previous tactic doesn’t work, you can edit the file autoexec.bat, which you can find (assuming<br />

that you have a relatively standard setup) in the top directory of the C drive. Open the file in Notepad (or the<br />

IDLE text editor, for that matter) and add a line setting the PYTHONPATH. If you want to add the directory<br />

C:\python you type the following:<br />

set PYTHONPATH=%PYTHONPATH%;C:\python<br />

For information on setting up Python in Mac OS, see the MacPython pages at http://cwi.nl/~jack/<br />

macpython.<br />

Depending on which operating system you are using, the contents of PYTHONPATH varies<br />

(see the sidebar “Environment Variables”), but basically it’s just like sys.path—a list of directories.<br />

■Tip You don’t have to change the sys.path by using PYTHONPATH. Path configuration files provide a<br />

useful shortcut to make Python do it for you. A path configuration file is a file with the file name extension<br />

.pth that contains directories that should be added to sys.path; empty lines and lines beginning with # are<br />

ignored. Files beginning with import are executed.<br />

For a path configuration file to be executed, it must be placed in a directory where it can be found. For<br />

Windows, use the directory named by sys.prefix (probably something like C:\Python22), and in UNIX,<br />

use the site-packages directory. (For more information, look up the site module in the Python Library<br />

Reference. This module is automatically imported during initialization of the Python interpreter.)

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

Saved successfully!

Ooh no, something went wrong!