Welcome, guest | Sign In | My Account | Store | Cart

r'''Initialize the Python runtime environment when the interpreter starts.

Local modules are kept in a PythonLib directory instead of site-packages, so they are not affected by updating, replacing, or removing the Python directory tree under Windows.

Python, 51 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Startup.py
r'''Initialize the Python runtime environment when the interpreter starts.

Local modules are kept in a PythonLib directory instead of site-packages,
so they are not affected by updating, replacing, or removing the Python
directory tree under Windows.

This program appends the PythonLib directory, where this program resides,
to sys.path.  It also appends all the first-level subdirectories except
PythonLib\Override.

If directory PythonLib\Override exists insert it in sys.path immediately
after the current directory and before everything else, so it can be used
to override standard modules if necessary.

Otherwise make minimal changes to the Python runtime environment, so
scripts developed on this system will be portable.

To use this program create a PythonLib directory; copy this program into
it; and set environment variable PYTHONSTARTUP to the fully qualified
pathname of this program, e.g. 'C:\Programs\PythonLib\Startup.py'.

Copyright 2001 by James M Jinkins.  Released under the Python License.
'''

# Import new language features from __future__ to insure that programs
# developed on this system will run under Python 2.2.
# For older code this conflicts with "minimal changes to the Python
# runtime environment."
# from __future__ import division
# from __future__ import generators
# from __future__ import nested_scopes

# Add directories to sys.path
import os, sys
startupDir = os.path.dirname(os.getenv("PYTHONSTARTUP"))
sys.path.append(startupDir)
for file in os.listdir(startupDir):
    path = os.path.join(startupDir, file)
    if os.path.isdir(path):
        if file.lower() == "override":
            sys.path.insert(2, path)
        else:
            sys.path.append(path)

# Cleanup the global namespace
del(sys)
del(os)
del(startupDir)
del(file)
del(path)

Under Windows a new version of Python is as likely to be installed in a new directory tree as to overwrite an old one. Keeping non-batteries-included and locally written modules in a separate Python library simplifies this.

Just set environment variable PYTHONSTARTUP to the fully qualified pathname of this program in the PythonLib directory.