title: What sets up sys.path with Python, and when?
body: <p>When I run </p>
  
  <pre><code>import sys 
  print sys.path
  </code></pre>
  
  <p>on my Mac (Mac OS X 10.6.5, Python 2.6.1), I get the following results.</p>
  
  <pre>
  /Library/Python/2.6/site-packages/ply-3.3-py2.6.egg
  ...
  /Library/Python/2.6/site-packages/ipython-0.10.1-py2.6.egg
  /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip
  /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
  /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin
  /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac
  /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages
  
  /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python
  /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk
  /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old
  /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload
  /Library/Python/2.6/site-packages
  /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC
  /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode
  </pre>
  
  <p>They are grouped into 5 categories.</p>
  
  <ul>
  <li>/Library/Python/2.6/site-packages/*.egg</li>
  <li>/Library/Python/2.6/site-packages</li>
  <li>Frameworks/Python.framework/Versions/2.6/lib/python2.6</li>
  <li>Frameworks/Python.framework/Versions/2.6/Extras/lib/python</li>
  <li>PATH from PYTHONPATH environment variable.</li>
  </ul>
  
  <p>And I can add more paths using the code </p>
  
  <pre><code>sys.path.insert(0, MORE_PATH)
  </code></pre>
  
  <ul>
  <li>What routines sets up those paths, and when?</li>
  <li>Are some of the paths are built in python source code?</li>
  <li>Is it possible that the paths inserted with 'sys.path.insert' are ignored? I'm curious about this, as with mod_wsgi, I found the paths are not found with 'sys.path.insert'. I asked <a href="https://stackoverflow.com/questions/4269445/does-django-or-mod-wsgi-modify-sys-path-when-its-running">another post</a> for this question.</li>
  </ul>
  
  <h2>ADDED</h2>
  
  <p>Based on Michael's answer, I looked into site.py, and I got the following code.</p>
  
  <pre><code>def addsitepackages(known_paths):
      """Add site-packages (and possibly site-python) to sys.path"""
      sitedirs = []
      seen = []
  
      for prefix in PREFIXES:
          if not prefix or prefix in seen:
              continue
          seen.append(prefix)
  
          if sys.platform in ('os2emx', 'riscos'):
              sitedirs.append(os.path.join(prefix, "Lib", "site-packages"))
          elif sys.platform == 'darwin' and prefix == sys.prefix:
              sitedirs.append(os.path.join("/Library/Python", sys.version[:3], "site-packages"))
  </code></pre>
  
  <p>I also think that the directory name that has site.py (/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6 for my Mac) should be built into Python source code.</p>