简体   繁体   English

如何忽略〜/ .local / lib / python2.7 / site-packages中的python模块?

[英]How to ignore python module in ~/.local/lib/python2.7/site-packages?

I have a python package dateutil in the ~/.local/lib/python2.7/site-packages directory as well as one in /usr/lib/python2.7/dist-packages/dateutil . 我有一个Python包dateutil~/.local/lib/python2.7/site-packages目录,以及一个在/usr/lib/python2.7/dist-packages/dateutil When I try to run an executable from kalibr (a camera calibration tool, I downloaded the CDE), rrule.py from the dateutil package in ~/.local/lib/python2.7/site-packages throws an error "ImportError: No module named fractions". 当我尝试运行从可执行kalibr (照相机校正工具,我下载了CDE), rrule.pydateutil包在~/.local/lib/python2.7/site-packages抛出一个错误“导入错误:没有名为分数的模块”。 I can get kalibr running, by removing said dateutil package (st it uses the one in /usr/lib/python2.7/dist-packages/dateutil ) but I fear that this will have some implications on other projects. 我可以通过删除所说的dateutil包(它使用/usr/lib/python2.7/dist-packages/dateutil那个)来使kalibr运行,但是我担心这会对其他项目产生影响。

So I figured that the basic problem here is that python prefers the dateutil package in ~/.local/lib/python2.7/site-packages over the one in /usr/lib/python2.7/dist-packages/dateutil . 所以我想,这里的根本问题是,蟒蛇喜欢的dateutil包在~/.local/lib/python2.7/site-packages在一个在/usr/lib/python2.7/dist-packages/dateutil

How can I make python prefer the latter one? 我怎样才能使python更喜欢后一种呢?

(I am using Ubuntu 16.04) (我正在使用Ubuntu 16.04)

I don't think there's a way to choose which directory to load packages from on a per-package basis, but you can change the order in which Python looks for packages. 我不认为有一种方法可以选择在每个软件包的基础上从哪个目录加载软件包,但是您可以更改Python查找软件包的顺序。 The order is reflected in sys.path . 该顺序反映在sys.path The default package search path is influenced by site-specific files , but mostly it's: 默认程序包搜索路径受特定站点的文件影响 ,但主要是:

  1. The current directory. 当前目录。
  2. Directories listed in the PYTHONPATH environment variable. PYTHONPATH环境变量中列出的目录。
  3. The standard library that comes with Python itself. Python本身随附的标准库。
  4. The dist-packages directory under ~/.local (the user site-packages directory ), which is where pip install --user installs things. ~/.local下的dist-packages目录( 用户site-packages目录 ),这是pip install --user安装内容的位置。 You can change the path to this directory by setting the environment variable PYTHONUSERBASE and disable it completely by setting PYTHONNOUSERSITE to a non-empty value. 您可以通过设置环境变量PYTHONUSERBASE来更改此目录的路径,并可以通过将PYTHONNOUSERSITE设置为非空值来完全禁用该路径。
  5. The dist-packages directory under /usr/local , which is where pip install installs things. /usr/local下的dist-packages目录是pip install安装的目录。

So if a directory needs to be traversed first, you can put it in PYTHONPATH . 因此,如果需要首先遍历目录,则可以将其放在PYTHONPATH In your case, there's a good chance that you can just disable ~/.local to run kalibr. 在您的情况下,很有可能仅禁用~/.local来运行kalibr。

env PYTHONNOUSERSITE=1 kalibr

To see the package search path, run 要查看包搜索路径,请运行

python -m site

(All of this applies to Python3 as well.) (所有这些同样适用于Python3。)

(Generally, preferring packages under ~/.local to packages under /usr is the right thing. Ideally, packages would always be backward compatible, and if you ran into an incompatibility, you'd just install the latest version under ~/.local with pip install --user . But in the real world, this doesn't always work out.) (通常,优先选择~/.local下的软件包而不是/usr下的软件包。理想情况下,软件包始终向后兼容,如果遇到不兼容的情况,您只需将最新版本安装在~/.local使用pip install --user 。但是在现实世界中,这并不总是可行的。)

See the answers here about changing the PYTHONPATH and the default module search order. 请参阅此处有关更改PYTHONPATH和默认模块搜索顺序的答案 You should also check your script to see if it modifies either PYTHONPATH or sys.path . 您还应该检查脚本,看看它是否修改了PYTHONPATHsys.path

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Linux下的/usr/local/lib/python2.7/site-packages下安装Pip - how to Install Pip under /usr/local/lib/python2.7/site-packages in Linux ImportError:dlopen(/usr/local/lib/python2.7/site-packages/_geoslib.so - ImportError: dlopen(/usr/local/lib/python2.7/site-packages/_geoslib.so 如何手动将文件夹复制到/app/.heroku/python/lib/python2.7/site-packages/? - How to manually copy folder to /app/.heroku/python/lib/python2.7/site-packages/? 将模块导入python脚本时如何忽略.local/lib/python3.7/site-packages/ - How to ignore .local/lib/python3.7/site-packages/ when importing a module to python script Python OSX - /usr/local/lib/python2.7/site-packages/easysnmp/interface.so中的平面命名空间 - Python OSX - flat namespace in /usr/local/lib/python2.7/site-packages/easysnmp/interface.so 找不到:/lib/python2.7/site-packages/tensorflow/tensorboard/lib/svg/summary-icon.sv - Not found : /lib/python2.7/site-packages/tensorflow/tensorboard/lib/svg/summary-icon.sv “/lib/python2.7/site-packages/不支持.pth文件”在MacOS上安装pip - “/lib/python2.7/site-packages/ does NOT support .pth files” installing pip on MacOS 没有VirtualEnv的Python2.7中的站点包 - Site-Packages in Python2.7 without VirtualEnv 如何为本地Python 2.7安装设置site-packages目录 - How to set site-packages directory for local Python 2.7 install 安装python settuptools时发生错误-没有这样的文件或目录:'/usr/local/lib/python2.7/site-packages/test-easy-install-8811.pth' - Error when installing python settuptools - No such file or directory: '/usr/local/lib/python2.7/site-packages/test-easy-install-8811.pth'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM