如何让pip指向更新版本的Python

6

我在我的centOS服务器上安装了两个版本的Python

[ethan@demo ~]$ python2.6 --version
Python 2.6.6
[ehtan@demo ~]$ python --version
Python 2.7.3

一些重要的centOS软件包需要较旧版本(2.6),因此我无法删除它。

使用pip安装软件包时,它们将被安装在Python 2.6中。然而,我希望它们被安装到Python 2.7中。

我该如何改变这种行为呢?

例如,当我尝试安装Wand时,以下是发生的情况:

[ethan@demo ~]$ pip install Wand
Requirement already satisfied (use --upgrade to upgrade): Wand in /usr/lib/python2.6/site-packages
Cleaning up...
[ethan@demo ~]$ python2.6
Python 2.6.6 (r266:84292, Jul 10 2013, 22:48:45) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wand
>>> exit()
[ethan@demo ~]$ python
Python 2.7.3 (default, Oct 11 2013, 15:59:28) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wand
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named wand
>>> exit()

编辑

我找到了这个答案,但它对我没用 https://dev59.com/enE85IYBdhLWcg3wXCS1#4910393


2
每个 Python 的安装应该都有一个 pip 程序,因此您需要访问专门针对更新版本的那个。 - wnnmaw
你能使用虚拟环境吗?虚拟环境可以使用特定的Python解释器进行初始化,它们使得管理多个Python版本和多个依赖项变得更加容易。 - dm03514
@wnnmaw 我只有pip,没有pip-2.6、pip-2.7等。当我执行 pip install --upgrade pip 命令时,输出为 Requirement already up-to-date: pip in /usr/lib/python2.6/site-packages Cleaning up... - Ethan
@Ethan,你需要两个版本的pip,每个版本对应Python的一个版本。因此,你有pip-2.6。 - wnnmaw
1个回答

5

您需要分别为每个Python版本安装pip。

为了安装Python2.7的pip,请运行

sudo easy_install-2.7 pip

使用pip-2.7安装Wand

sudo pip-2.7 install Wand


也许这就是问题所在。$ sudo easy_install-2.7 pip sudo: easy_install-2.7: command not found 请查看此Gist https://gist.github.com/Omnipresent/9687245。 - Ethan
你需要验证$PATH是否有效,并且还包括root用户的easy_install-2.7目录。当切换到root用户时,需要将$PATH设置为包括easy_install-2.7目录。 - Forge
1
好险,我通过提供绝对路径让它正常工作了:sudo /usr/local/bin/easy_install-2.7 pip 然后 sudo /usr/local/bin/pip2.7 install Wand。现在我可以成功地导入Wand了。谢谢 - Ethan
它是 pip2.7(没有连字符)...至少在Centos SCL中是这样。 - Dexter Legaspi
使用绝对路径,例如/usr/local/bin/easy_install,可以正常工作!感谢@Ethan。 - Mukesh Chapagain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接