Jun
04
2009
0

PYTHONSTARTUP == .profile

When you use Python inter­ac­tively, it is fre­quently handy to have some stan­dard com­mands exe­cuted every time the inter­preter is started. You can do this by set­ting an envi­ron­ment vari­able named PYTHONSTARTUP to the name of a file con­tain­ing your start-up com­mands. This is sim­i­lar to the .pro­file fea­ture of the Unix shells.

This file is only read in inter­ac­tive ses­sions, not when Python reads com­mands from a script, and not when /dev/tty is given as the explicit source of com­mands (which oth­er­wise behaves like an inter­ac­tive ses­sion). It is exe­cuted in the same name­space where inter­ac­tive com­mands are exe­cuted, so that objects that it defines or imports can be used with­out qual­i­fi­ca­tion in the inter­ac­tive ses­sion. You can also change the prompts sys.ps1 and sys.ps2 in this file.

If you want to read an addi­tional start-up file from the cur­rent direc­tory, you can pro­gram this in the global start-up file using code like if os.path.isfile(‘.pythonrc.py’): execfile(‘.pythonrc.py’). If you want to use the startup file in a script, you must do this explic­itly in the script:

import os
file­name = os.environ.get(‘PYTHONSTARTUP’)
if file­name and os.path.isfile(filename):
execfile(filename)

Written by satyag in: python,tips | Tags:

Powered by WordPress. Theme: TheBuckmaker:
Fatal error: Call to undefined function autocopyright() in /home/satya/public_html/wp-content/themes/aerodrome/footer.php on line 9