user.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """Hook to allow user-specified customization code to run.
  2. As a policy, Python doesn't run user-specified code on startup of
  3. Python programs (interactive sessions execute the script specified in
  4. the PYTHONSTARTUP environment variable if it exists).
  5. However, some programs or sites may find it convenient to allow users
  6. to have a standard customization file, which gets run when a program
  7. requests it. This module implements such a mechanism. A program
  8. that wishes to use the mechanism must execute the statement
  9. import user
  10. The user module looks for a file .pythonrc.py in the user's home
  11. directory and if it can be opened, execfile()s it in its own global
  12. namespace. Errors during this phase are not caught; that's up to the
  13. program that imports the user module, if it wishes.
  14. The user's .pythonrc.py could conceivably test for sys.version if it
  15. wishes to do different things depending on the Python version.
  16. """
  17. from warnings import warnpy3k
  18. warnpy3k("the user module has been removed in Python 3.0", stacklevel=2)
  19. del warnpy3k
  20. import os
  21. home = os.curdir # Default
  22. if 'HOME' in os.environ:
  23. home = os.environ['HOME']
  24. elif os.name == 'posix':
  25. home = os.path.expanduser("~/")
  26. elif os.name == 'nt': # Contributed by Jeff Bauer
  27. if 'HOMEPATH' in os.environ:
  28. if 'HOMEDRIVE' in os.environ:
  29. home = os.environ['HOMEDRIVE'] + os.environ['HOMEPATH']
  30. else:
  31. home = os.environ['HOMEPATH']
  32. pythonrc = os.path.join(home, ".pythonrc.py")
  33. try:
  34. f = open(pythonrc)
  35. except IOError:
  36. pass
  37. else:
  38. f.close()
  39. execfile(pythonrc)