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.

CHAPTER 10 ■ BATTERIES INCLUDED 209<br />

ENVIRONMENT VARIABLES<br />

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

they are like <strong>Python</strong> variables, but they are set outside the <strong>Python</strong> interpreter. To find out how <strong>to</strong> 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 direc<strong>to</strong>ry. Add the following <strong>to</strong> that<br />

file <strong>to</strong> add the direc<strong>to</strong>ry ~/python <strong>to</strong> your PYTHONPATH:<br />

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

Note that multiple direc<strong>to</strong>ries 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 <strong>to</strong> 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 <strong>to</strong> edit your au<strong>to</strong>exec.bat file instead, as covered in the next paragraph).<br />

<strong>From</strong> 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 but<strong>to</strong>n. 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 direc<strong>to</strong>ry as the value. Note that multiple<br />

direc<strong>to</strong>ries are separated by semicolons.<br />

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

that you have a relatively standard setup) in the <strong>to</strong>p direc<strong>to</strong>ry of the C drive. Open the file in Notepad (or the<br />

IDLE text edi<strong>to</strong>r, for that matter) and add a line setting the PYTHONPATH. If you want <strong>to</strong> add the direc<strong>to</strong>ry<br />

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

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

For information on setting up <strong>Python</strong> in Mac OS, see the Mac<strong>Python</strong> 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 direc<strong>to</strong>ries.<br />

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

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

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

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

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

Windows, use the direc<strong>to</strong>ry named by sys.prefix (probably something like C:\<strong>Python</strong>22), and in UNIX,<br />

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

Reference. This module is au<strong>to</strong>matically imported during initialization of the <strong>Python</strong> interpreter.)

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

Saved successfully!

Ooh no, something went wrong!