Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex

26.12user -- User-specific configuration hook

As a policy, Python doesn't run user-specified code on startup ofPython programs. (Only interactive sessions execute the scriptspecified in thePYTHONSTARTUP environment variable if itexists).

However, some programs or sites may find it convenient to allow usersto have a standard customization file, which gets run when a programrequests it. This module implements such a mechanism. A programthat wishes to use the mechanism must execute the statement

import user

Theuser module looks for a file.pythonrc.py in the user'shome directory and if it can be opened, executes it (usingexecfile()) in its own (themoduleuser's) global namespace. Errors during this phaseare not caught; that's up to the program that imports theuser module, if it wishes. The home directory is assumed tobe named by theHOME environment variable; if this is not set,the current directory is used.

The user's.pythonrc.py could conceivably test forsys.version if it wishes to do different things depending onthe Python version.

A warning to users: be very conservative in what you place in your.pythonrc.py file. Since you don't know which programs willuse it, changing the behavior of standard modules or functions isgenerally not a good idea.

A suggestion for programmers who wish to use this mechanism: a simpleway to let users specify options for your package is to have themdefine variables in their.pythonrc.py file that you test inyour module. For example, a modulespam that has a verbositylevel can look for a variableuser.spam_verbose, as follows:

import userverbose = bool(getattr(user, "spam_verbose", 0))

(The three-argument form ofgetattr() is used in casethe user has not definedspam_verbose in their.pythonrc.py file.)

Programs with extensive customization needs are better off reading aprogram-specific customization file.

Programs with security or privacy concerns shouldnot importthis module; a user can easily break into a program by placingarbitrary code in the.pythonrc.py file.

Modules for general use shouldnot import this module; it mayinterfere with the operation of the importing program.

See Also:

Modulesite:
Site-wide customization mechanism.


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp