有没有办法让pip在多个版本的Python中都能很好地运行?例如,我想使用pip显式地将东西安装到我的站点2.5安装或站点2.6安装中。

例如,对于easy_install,我使用easy_install-2.{5,6}。

是的,我知道virtualenv,不,它不是这个特殊问题的解决方案。


当前回答

网址:https://docs.python.org/3/installing/

下面是如何为同时安装的linux, mac, posix的不同版本安装包:

python2   -m pip install SomePackage  # default Python 2
python2.7 -m pip install SomePackage  # specifically Python 2.7
python3   -m pip install SomePackage  # default Python 3
python3.4 -m pip install SomePackage  # specifically Python 3.4
python3.5 -m pip install SomePackage  # specifically Python 3.5
python3.6 -m pip install SomePackage  # specifically Python 3.6

在Windows上,将py Python启动器与-m开关结合使用:

py -2   -m pip install SomePackage  # default Python 2
py -2.7 -m pip install SomePackage  # specifically Python 2.7
py -3   -m pip install SomePackage  # default Python 3
py -3.4 -m pip install SomePackage  # specifically Python 3.4

其他回答

例如,您可以转到C:\Python2.7\Scripts,然后从该路径运行cmd。然后你可以运行pip2.7安装你的包…

这将为该版本的Python安装包。

在Linux、Mac OS X和其他POSIX系统上,使用带版本控制的Python命令和-m开关来运行pip的适当副本:

python2.7 -m pip install SomePackage
python3.4 -m pip install SomePackage

(也可以使用版本适当的PIP命令)

在Windows上,将py Python启动器与-m开关结合使用:

py -2.7 -m pip install SomePackage  # specifically Python 2.7
py -3.4 -m pip install SomePackage  # specifically Python 3.4

如果py -3.4有错误,那么试试:

pip install SomePackage

具体到windows: \path\to\python.exe -m pip install PackageName works

以下是我对这个问题的看法。适用于Python3。主要特点有:

每个Python版本都是从源代码编译的 所有版本都安装在本地 不会以任何方式破坏系统的默认Python安装 每个Python版本都使用virtualenv进行隔离

前提条件:如果你正在使用一些基本的瘦客户端,没有安装额外的turf,你应该首先运行这个(至少在ubuntu 18.04中,为了方便,额外的包添加了):

sudo apt-get update
sudo apt-get install software-properties-common
sudo apt-add-repository universe
sudo apt-get update
sudo apt-get install -y build-essential cmake

sudo apt-get install -y zlib1g zlib1g-dev libsqlite3-dev \
openssl libssl-dev libffi-dev unzip pciutils net-tools \
libblas-dev gfortran libblas3 

具体步骤如下:

If you have several extra python versions installed in some other way, get rid of them, e.g., remove $HOME/.local/lib/python3.x, etc. (also the globally installed ones). Don't touch your system's default python3 version though. Download source for different python versions under the following directory structure: $HOME/ python_versions/ : download Python-*.tgz packages here and "tar xvf" them. You'll get directories like this: Python-3.4.8/ Python-3.6.5/ Python-3.x.y/ ... At each "Python-3.x.y/" directory, do the following (do NOT use "sudo" in any of the steps!): mkdir root ./configure --prefix=$PWD/root make -j 2 make install virtualenv --no-site-packages -p root/bin/python3.x env At "python_versions/" create files like this: env_python3x.bash: #!/bin/bash echo "type deactivate to exit" source $HOME/python_versions/Python-3.x.y/env/bin/activate Now, anytime you wish to opt for python3.x, do source $HOME/python_versions/env_python3x.bash

进入virtualenv

在virtualenv中,安装你喜欢的python包 PIP install——upgrade package_name 要退出virtualenv和python版本,只需输入“deactivate”

更新

似乎不赞成使用无站点包。有一个简单的解决方法:一旦你激活了virtualenv,只要将HOME env变量指向其他地方,而不是你实际的主目录,即:

export HOME=some/where/else

一般来说,一个很好的方法是:

创建virtualenv 激活virtualenv 如果你想“回收”现有的库到你的virtualenv,软链接它们从你现有的安装,即。 ln -s $HOME/.local/lib/python3.6/site-packages/numpy $PWD/venv/lib/python3.6/site-packages/ export PYTHONPATH=, export HOME=/some/other/dir

现在您应该有了自定义隔离的virtualenv。

更新2 / sudo

不想强迫sudo使用你的virtualenv吗?

Defaults        secure_path="/home/USENAME/Python-3.x.y/env/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
Defaults        env_keep += "VIRTUAL_ENV"
Defaults        env_keep += "PYTHONPATH"

现在尝试“sudo python3—version”,魔术应该会发生

更新3 / docker

在docker中启用virtualenv(当然,你已经在docker镜像中构建了它):

ENV VIRTUAL_ENV=/home/USER/Python-3.x.y/env
ENV PYTHONPATH=
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

显然,easy_install和pip有多个版本。看起来很乱。不管怎样,这是我在Ubuntu 12.10上安装Django for Python 2.7的方法:

$ sudo easy_install-2.7 pip
Searching for pip
Best match: pip 1.1
Adding pip 1.1 to easy-install.pth file
Installing pip-2.7 script to /usr/local/bin

Using /usr/lib/python2.7/dist-packages
Processing dependencies for pip
Finished processing dependencies for pip

$ sudo pip-2.7 install django
Downloading/unpacking django
  Downloading Django-1.5.1.tar.gz (8.0Mb): 8.0Mb downloaded
  Running setup.py egg_info for package django

    warning: no previously-included files matching '__pycache__' found under directory '*'
    warning: no previously-included files matching '*.py[co]' found under directory '*'
Installing collected packages: django
  Running setup.py install for django
    changing mode of build/scripts-2.7/django-admin.py from 644 to 755

    warning: no previously-included files matching '__pycache__' found under directory '*'
    warning: no previously-included files matching '*.py[co]' found under directory '*'
    changing mode of /usr/local/bin/django-admin.py to 755
Successfully installed django
Cleaning up...

$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>>