更改 python sys.path 的优先级

编程入门 行业动态 更新时间:2024-10-27 18:30:22
本文介绍了更改 python sys.path 的优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在 python 2.7 中更改 sys.path 中路径的优先级?我知道我可以使用 PYTHONPATH 环境变量,但这就是我会得到的:

How can I change the priority of the path in sys.path in python 2.7? I know that I can use PYTHONPATH environment variable, but it is what I will get:

$ PYTHONPATH=/tmp python Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> for i in sys.path: ... print i ... /usr/local/lib/python2.7/dist-packages/pycuda-2014.1-py2.7-linux-x86_64.egg /usr/local/lib/python2.7/dist-packages/pytest-2.6.2-py2.7.egg /usr/local/lib/python2.7/dist-packages/pytools-2014.3-py2.7.egg /usr/local/lib/python2.7/dist-packages/py-1.4.24-py2.7.egg /usr/lib/python2.7/dist-packages /tmp /usr/lib/python2.7 /usr/lib/python2.7/plat-x86_64-linux-gnu /usr/lib/python2.7/lib-tk /usr/lib/python2.7/lib-old /usr/lib/python2.7/lib-dynload /usr/local/lib/python2.7/dist-packages /usr/lib/python2.7/dist-packages/PILcompat /usr/lib/python2.7/dist-packages/gtk-2.0 /usr/lib/python2.7/dist-packages/ubuntu-sso-client >>>

/tmp 添加在/usr/lib/python2.7/dist-packages和/usr/lib/python2.7之间.

我的目标是让python首先从/usr/local/lib/python2.7/dist-packages加载包.

My goal is to make python to load packages from /usr/local/lib/python2.7/dist-packages first.

这是我想要的:

$ python Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import numpy as np >>> np.version <module 'numpy.version' from '/usr/local/lib/python2.7/dist-packages/numpy/version.pyc'> >>>

如果我通过 apt-get install python-numpy 安装 python-numpy.Python 将尝试从 /usr/lib/python2.7 而不是我编译的那个加载.

If I install python-numpy by apt-get install python-numpy. Python will try to load from /usr/lib/python2.7 and not the one I compiled.

推荐答案

如您所知,sys.path 初始化自:

As you may know, sys.path is initialized from:

  • 当前目录
  • 你的PYTHONPATH
  • 依赖于安装的默认值

然而不幸的是,这只是故事的一部分:setuptools 创建了 easy-install.pth 文件,这些文件也修改了 sys.path,最糟糕的是它们前置包,因此完全搞乱了目录.

However unfortunately that is only part of the story: setuptools creates easy-install.pth files, which also modify sys.path and worst of all they prepend packages and therefore totally mess up the order of directories.

特别是(至少在我的系统上),/usr/local/lib/python2.7/dist-packages/easy-install.pth 包含以下内容:

In particular (at least on my system), there is /usr/local/lib/python2.7/dist-packages/easy-install.pth with the following contents:

import sys; sys.__plen = len(sys.path) /usr/lib/python2.7/dist-packages import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)

这会导致 /usr/lib/python2.7/dist-packages 甚至在您的 PYTHONPATH 之前被预置!

This causes /usr/lib/python2.7/dist-packages to be prepended even before your PYTHONPATH!

您可以做的只是将此文件中的第二行更改为

What you could do is simply change the 2nd line in this file to

/usr/local/lib/python2.7/dist-packages

你就会得到你想要的优先级.

and you will get your desired priority.

但是请注意,此文件可能会被未来的 setuptools 调用覆盖或再次更改!

However beware this file might be overwritten or changed again by a future setuptools invocation!

更多推荐

更改 python sys.path 的优先级

本文发布于:2023-11-23 21:19:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622896.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:优先级   python   sys   path

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!