GIF89a;
Direktori : /usr/share/doc/python-docs-2.7.5/html/library/ |
Current File : //usr/share/doc/python-docs-2.7.5/html/library/user.html |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>27.15. user — User-specific configuration hook — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="27. Python Runtime Services" href="python.html" /> <link rel="next" title="27.16. fpectl — Floating point exception control" href="fpectl.html" /> <link rel="prev" title="27.14. site — Site-specific configuration hook" href="site.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="fpectl.html" title="27.16. fpectl — Floating point exception control" accesskey="N">next</a> |</li> <li class="right" > <a href="site.html" title="27.14. site — Site-specific configuration hook" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="python.html" accesskey="U">27. Python Runtime Services</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-user"> <span id="user-user-specific-configuration-hook"></span><h1>27.15. <a class="reference internal" href="#module-user" title="user: A standard way to reference user-specific modules. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">user</span></tt></a> — User-specific configuration hook<a class="headerlink" href="#module-user" title="Permalink to this headline">¶</a></h1> <p class="deprecated"> <span class="versionmodified">Deprecated since version 2.6: </span>The <a class="reference internal" href="#module-user" title="user: A standard way to reference user-specific modules. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">user</span></tt></a> module has been removed in Python 3.</p> <p id="index-0">As a policy, Python doesn’t run user-specified code on startup of Python programs. (Only interactive sessions execute the script specified in the <span class="target" id="index-1"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONSTARTUP"><tt class="xref std std-envvar docutils literal"><span class="pre">PYTHONSTARTUP</span></tt></a> environment variable if it exists).</p> <p>However, some programs or sites may find it convenient to allow users to have a standard customization file, which gets run when a program requests it. This module implements such a mechanism. A program that wishes to use the mechanism must execute the statement</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">user</span> </pre></div> </div> <p id="index-2">The <a class="reference internal" href="#module-user" title="user: A standard way to reference user-specific modules. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">user</span></tt></a> module looks for a file <tt class="file docutils literal"><span class="pre">.pythonrc.py</span></tt> in the user’s home directory and if it can be opened, executes it (using <a class="reference internal" href="functions.html#execfile" title="execfile"><tt class="xref py py-func docutils literal"><span class="pre">execfile()</span></tt></a>) in its own (the module <a class="reference internal" href="#module-user" title="user: A standard way to reference user-specific modules. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">user</span></tt></a>‘s) global namespace. Errors during this phase are not caught; that’s up to the program that imports the <a class="reference internal" href="#module-user" title="user: A standard way to reference user-specific modules. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">user</span></tt></a> module, if it wishes. The home directory is assumed to be named by the <span class="target" id="index-3"></span><tt class="xref std std-envvar docutils literal"><span class="pre">HOME</span></tt> environment variable; if this is not set, the current directory is used.</p> <p>The user’s <tt class="file docutils literal"><span class="pre">.pythonrc.py</span></tt> could conceivably test for <tt class="docutils literal"><span class="pre">sys.version</span></tt> if it wishes to do different things depending on the Python version.</p> <p>A warning to users: be very conservative in what you place in your <tt class="file docutils literal"><span class="pre">.pythonrc.py</span></tt> file. Since you don’t know which programs will use it, changing the behavior of standard modules or functions is generally not a good idea.</p> <p>A suggestion for programmers who wish to use this mechanism: a simple way to let users specify options for your package is to have them define variables in their <tt class="file docutils literal"><span class="pre">.pythonrc.py</span></tt> file that you test in your module. For example, a module <tt class="xref py py-mod docutils literal"><span class="pre">spam</span></tt> that has a verbosity level can look for a variable <tt class="docutils literal"><span class="pre">user.spam_verbose</span></tt>, as follows:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">user</span> <span class="n">verbose</span> <span class="o">=</span> <span class="nb">bool</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">user</span><span class="p">,</span> <span class="s">"spam_verbose"</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span> </pre></div> </div> <p>(The three-argument form of <a class="reference internal" href="functions.html#getattr" title="getattr"><tt class="xref py py-func docutils literal"><span class="pre">getattr()</span></tt></a> is used in case the user has not defined <tt class="docutils literal"><span class="pre">spam_verbose</span></tt> in their <tt class="file docutils literal"><span class="pre">.pythonrc.py</span></tt> file.)</p> <p>Programs with extensive customization needs are better off reading a program-specific customization file.</p> <p>Programs with security or privacy concerns should <em>not</em> import this module; a user can easily break into a program by placing arbitrary code in the <tt class="file docutils literal"><span class="pre">.pythonrc.py</span></tt> file.</p> <p>Modules for general use should <em>not</em> import this module; it may interfere with the operation of the importing program.</p> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt>Module <a class="reference internal" href="site.html#module-site" title="site: Module responsible for site-specific configuration."><tt class="xref py py-mod docutils literal"><span class="pre">site</span></tt></a></dt> <dd>Site-wide customization mechanism.</dd> </dl> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="site.html" title="previous chapter">27.14. <tt class="docutils literal"><span class="pre">site</span></tt> — Site-specific configuration hook</a></p> <h4>Next topic</h4> <p class="topless"><a href="fpectl.html" title="next chapter">27.16. <tt class="docutils literal"><span class="pre">fpectl</span></tt> — Floating point exception control</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/user.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="fpectl.html" title="27.16. fpectl — Floating point exception control" >next</a> |</li> <li class="right" > <a href="site.html" title="27.14. site — Site-specific configuration hook" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="python.html" >27. Python Runtime Services</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>