How is the python module search path determined on Mac OS X?

こ雲淡風輕ζ 提交于 2019-12-04 21:50:12

问题


When a non built-in module is imported, the interpreter searches in the locations given by sys.path. sys.path is initialized from these locations (http://docs.python.org/library/sys.html#sys.path):

  1. the directory containing the input script (or the current directory)
  2. PYTHONPATH
  3. the installation-dependent default

While the first two sources are straight-forward, can anyone explain how the third one works, and what possibilities there are for influencing it?

Although I would be interested in a general solution, my specific issues are:

  • I have installed the Enthought distribution 7.2 32-bit, and then Scipy-Superpack. Now enthought python tries to import numpy from /Library/Python/2.7/, which is where superpack installed them, instead of from the enthought site-packages.
  • a wxPython application created with py2app -A does not have the same sys.path as when starting the application with python start_app.py.

回答1:


The basis of the third source is set at compile time (or configure time, more precisely), depending on the platform. It is then expanded and added to at run-time via .pth files, etc. (which is something you can do once the Python executable is compiled to influence that third source).

This page of the Python documentation has all the information on how .pth files work, and also more information on how sys.path is constructed from build-time settings, etc. http://docs.python.org/library/site.html

I'm not sure why you want to influence that third source specifically though, when you can influence the whole sys.path. Anyhow, the three ways of influencing sys.path (without recompiling Python or patching the source code) are:

  1. Via the PYTHONPATH environment variable.
  2. By creating .pth files and dropping them where Python scans for packages. (See the link earlier for details.)
  3. Programmatically, by importing sys and then appending or prepending to sys.path

    import sys
    sys.path.insert(0, '/this/path/will/be/considered/first')
    

Hopefully one of these three ways should help you do what you want.




回答2:


On a Mac OS X 10.7.5 system, I also had the problem that Enthought python was looking for modules in /Library/Python/2.7/. Apparently, this was caused by the file easy-install.pth located in /Library/Python/2.7/site-packages. After changing the extension of this file Enthought python imported the Enthought modules (from /Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages).



来源:https://stackoverflow.com/questions/10955024/how-is-the-python-module-search-path-determined-on-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!