问题描述
在Python中,我收到一个错误消息,因为它正在从/usr/lib/python2.6/site-packages
加载模块,但我希望它在$HOME/python-modules/lib/python2.6/site-packages
中使用我的版本,该版本是使用pip-python --install-option="--prefix=$HOME/python-modules --ignore-installed
如何告诉Python使用我的库版本?将PYTHONPATH
设置为$HOME/python-modules/lib/python2.6/site-packages
并没有帮助,因为/usr/lib/...
显然具有优先权.
看看网站模块,用于自定义环境.
一种实现此目的的方法是将文件添加到当前在sys.path
上名为usercustomize.py
的位置,当Python启动时,它将自动导入该文件,您可以使用它来修改sys.path
. /p>
首先,将$PYTHONPATH
设置为$HOME
(如果$PYTHONPATH
具有值,则添加$HOME
),然后创建具有以下内容的文件$HOME/usercustomize.py
:
import sys, os
my_site = os.path.join(os.environ['HOME'],
'python-modules/lib/python2.6/site-packages')
sys.path.insert(0, my_site)
现在,当您启动Python时,应该在sys.path
系统默认值之前看到自定义site-packages目录.
In Python, I'm getting an error because it's loading a module from /usr/lib/python2.6/site-packages
but I'd like it to use my version in $HOME/python-modules/lib/python2.6/site-packages
, which I installed using pip-python --install-option="--prefix=$HOME/python-modules --ignore-installed
How can I tell Python to use my version of the library? Setting PYTHONPATH
to $HOME/python-modules/lib/python2.6/site-packages
doesn't help, since /usr/lib/...
apparently has precedence.
Take a look at the site module for ways to customize your environment.
One way to accomplish this is to add a file to a location currently on sys.path
called usercustomize.py
, when Python is starting up it will automatically import this file, and you can use it to modify sys.path
.
First, set $PYTHONPATH
to $HOME
(or add $HOME
if $PYTHONPATH
has a value), then create the file $HOME/usercustomize.py
with the following contents:
import sys, os
my_site = os.path.join(os.environ['HOME'],
'python-modules/lib/python2.6/site-packages')
sys.path.insert(0, my_site)
Now when you start Python you should see your custom site-packages directory before the system default on sys.path
.
这篇关于如何告诉Python比/usr/lib/python更喜欢$ HOME/lib/python中的模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!