在Mac OS X上如何确定python模块的搜索路径? [英] How is the python module search path determined on Mac OS X?

查看:307
本文介绍了在Mac OS X上如何确定python模块的搜索路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当导入非内置模块时,解释器将在sys.path给定的位置搜索.从这些位置初始化sys.path( http://docs.python.org/library/sys.html #sys.path ):

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. 包含输入脚本的目录(或当前目录)
  2. PYTHONPATH
  3. 与安装有关的默认设置

尽管前两个来源很简单,但是谁能解释第三个来源的工作方式以及影响它的可能性有多大?

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.

推荐答案

第三个源的基础是在编译时设置的(或更准确地说是在配置时),具体取决于平台.然后将其扩展并在运行时通过.pth文件等添加到该文件中.(一旦编译了Python可执行文件以影响第三个源代码,您就可以执行此操作).

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).

Python文档的此页面包含有关.pth文件如何工作的所有信息,以及有关如何根据构建时设置等构造sys.path的更多信息.

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

我不确定当您可以影响整个sys.path时,为什么还要特别影响第三个来源.无论如何,影响sys.path的三种方式(无需重新编译Python或修补源代码)是:

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. 通过PYTHONPATH环境变量.
  2. 通过创建.pth文件并将其拖放到Python扫描软件包的位置. (有关详细信息,请参阅前面的链接.)
  3. 以编程方式,通过导入sys然后添加或添加到sys.path

  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.

这篇关于在Mac OS X上如何确定python模块的搜索路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆