User Customization

Question

How can you make user customized settings to Python (e.g. path settings, package imports, etc.)?

Answer

The module user provides this feature. When imported, the module searches for your home directory and executes the file .pythonrc.py. The .pythonrc.py file is just a Python module with your personal settings. For instance, my file looks like this:

import sys
sys.path.append('/home/jlin/prog/python')
sys.path.append('/home/jlin/prog/python/general')

import IaGraph
from IaGraph import *

import Numeric as N
import MA

del sys

In the first section I add a few directories to my Python path in which I have my own packages and modules. In the second section I import all the functions of the IaGraph frontend which allows me to interactively make x-y and contour plots. Thirdly, I import the Numeric and MA packages, since I use those quite often. Finally, I free up the sys identifier.

As I said, adding the line:

import user

to your Python module or script should execute .pythonrc.py. You can also explicitly load the .pythonrc.py file:

execfile('/home/jlin/.pythonrc.py')

at the beginning of your Python modules and scripts. Note that if you're running an interactive session (e.g. you execute Python without any arguments) the .pythonrc.py file is loaded automatically.

Notes: See also Martelli (2003), p. 275.

Return to the Tips and Examples index page.

Valid HTML 4.01! Valid CSS! Updated: June 15, 2004 by Johnny Lin <email address>. License.