| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="module-builtin.html" /> |
| 13 | <link rel="prev" href="module-site.html" /> |
| 14 | <link rel="parent" href="python.html" /> |
| 15 | <link rel="next" href="module-builtin.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>3.31 user -- User-specific configuration hook</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="3.30 site " |
| 25 | href="module-site.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" |
| 28 | href="python.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="3.32 __builtin__ " |
| 31 | href="module-builtin.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="module-site.html">3.30 site </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="module-builtin.html">3.32 __builtin__ </A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION0053100000000000000000"> |
| 56 | 3.31 <tt class="module">user</tt> -- |
| 57 | User-specific configuration hook</A> |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | <A NAME="module-user"></A> |
| 62 | |
| 63 | <P> |
| 64 | <a id='l2h-808' xml:id='l2h-808'></a><a id='l2h-809' xml:id='l2h-809'></a> |
| 65 | <P> |
| 66 | As a policy, Python doesn't run user-specified code on startup of |
| 67 | Python programs. (Only interactive sessions execute the script |
| 68 | specified in the <a class="envvar" id='l2h-811' xml:id='l2h-811'>PYTHONSTARTUP</a> environment variable if it |
| 69 | exists). |
| 70 | |
| 71 | <P> |
| 72 | However, some programs or sites may find it convenient to allow users |
| 73 | to have a standard customization file, which gets run when a program |
| 74 | requests it. This module implements such a mechanism. A program |
| 75 | that wishes to use the mechanism must execute the statement |
| 76 | |
| 77 | <P> |
| 78 | <div class="verbatim"><pre> |
| 79 | import user |
| 80 | </pre></div> |
| 81 | |
| 82 | <P> |
| 83 | The <tt class="module">user</tt> module looks for a file <span class="file">.pythonrc.py</span> in the user's |
| 84 | home directory and if it can be opened, executes it (using |
| 85 | <tt class="function">execfile()</tt><a id='l2h-810' xml:id='l2h-810'></a>) in its own (the |
| 86 | module <tt class="module">user</tt>'s) global namespace. Errors during this phase |
| 87 | are not caught; that's up to the program that imports the |
| 88 | <tt class="module">user</tt> module, if it wishes. The home directory is assumed to |
| 89 | be named by the <a class="envvar" id='l2h-812' xml:id='l2h-812'>HOME</a> environment variable; if this is not set, |
| 90 | the current directory is used. |
| 91 | |
| 92 | <P> |
| 93 | The user's <span class="file">.pythonrc.py</span> could conceivably test for |
| 94 | <code>sys.version</code> if it wishes to do different things depending on |
| 95 | the Python version. |
| 96 | |
| 97 | <P> |
| 98 | A warning to users: be very conservative in what you place in your |
| 99 | <span class="file">.pythonrc.py</span> file. Since you don't know which programs will |
| 100 | use it, changing the behavior of standard modules or functions is |
| 101 | generally not a good idea. |
| 102 | |
| 103 | <P> |
| 104 | A suggestion for programmers who wish to use this mechanism: a simple |
| 105 | way to let users specify options for your package is to have them |
| 106 | define variables in their <span class="file">.pythonrc.py</span> file that you test in |
| 107 | your module. For example, a module <tt class="module">spam</tt> that has a verbosity |
| 108 | level can look for a variable <code>user.spam_verbose</code>, as follows: |
| 109 | |
| 110 | <P> |
| 111 | <div class="verbatim"><pre> |
| 112 | import user |
| 113 | |
| 114 | verbose = bool(getattr(user, "spam_verbose", 0)) |
| 115 | </pre></div> |
| 116 | |
| 117 | <P> |
| 118 | (The three-argument form of <tt class="function">getattr()</tt> is used in case |
| 119 | the user has not defined <code>spam_verbose</code> in their |
| 120 | <span class="file">.pythonrc.py</span> file.) |
| 121 | |
| 122 | <P> |
| 123 | Programs with extensive customization needs are better off reading a |
| 124 | program-specific customization file. |
| 125 | |
| 126 | <P> |
| 127 | Programs with security or privacy concerns should <em>not</em> import |
| 128 | this module; a user can easily break into a program by placing |
| 129 | arbitrary code in the <span class="file">.pythonrc.py</span> file. |
| 130 | |
| 131 | <P> |
| 132 | Modules for general use should <em>not</em> import this module; it may |
| 133 | interfere with the operation of the importing program. |
| 134 | |
| 135 | <P> |
| 136 | <div class="seealso"> |
| 137 | <p class="heading">See Also:</p> |
| 138 | |
| 139 | <dl compact="compact" class="seemodule"> |
| 140 | <dt>Module <b><tt class="module"><a href="module-site.html">site</a></tt>:</b> |
| 141 | <dd>Site-wide customization mechanism. |
| 142 | </dl> |
| 143 | </div> |
| 144 | |
| 145 | <DIV CLASS="navigation"> |
| 146 | <div class='online-navigation'> |
| 147 | <p></p><hr /> |
| 148 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 149 | <tr> |
| 150 | <td class='online-navigation'><a rel="prev" title="3.30 site " |
| 151 | href="module-site.html"><img src='../icons/previous.png' |
| 152 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 153 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" |
| 154 | href="python.html"><img src='../icons/up.png' |
| 155 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 156 | <td class='online-navigation'><a rel="next" title="3.32 __builtin__ " |
| 157 | href="module-builtin.html"><img src='../icons/next.png' |
| 158 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 159 | <td align="center" width="100%">Python Library Reference</td> |
| 160 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 161 | href="contents.html"><img src='../icons/contents.png' |
| 162 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 163 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 164 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 165 | <td class='online-navigation'><a rel="index" title="Index" |
| 166 | href="genindex.html"><img src='../icons/index.png' |
| 167 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 168 | </tr></table> |
| 169 | <div class='online-navigation'> |
| 170 | <b class="navlabel">Previous:</b> |
| 171 | <a class="sectref" rel="prev" href="module-site.html">3.30 site </A> |
| 172 | <b class="navlabel">Up:</b> |
| 173 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> |
| 174 | <b class="navlabel">Next:</b> |
| 175 | <a class="sectref" rel="next" href="module-builtin.html">3.32 __builtin__ </A> |
| 176 | </div> |
| 177 | </div> |
| 178 | <hr /> |
| 179 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 180 | </DIV> |
| 181 | <!--End of Navigation Panel--> |
| 182 | <ADDRESS> |
| 183 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 184 | </ADDRESS> |
| 185 | </BODY> |
| 186 | </HTML> |