[英]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.py
从dateutil
包在~/.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: 默认程序包搜索路径受特定于站点的文件影响 ,但主要是:
PYTHONPATH
environment variable. PYTHONPATH
环境变量中列出的目录。 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
设置为非空值来完全禁用该路径。 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
. 您还应该检查脚本,看看它是否修改了
PYTHONPATH
或sys.path
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.