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

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

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


当前回答

以下是我对这个问题的看法。适用于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
>>> 

例如,如果您将其他版本(例如3.5)设置为默认版本,并希望为python 2.7安装PIP:

在https://pypi.python.org/pypi/pip (tar)下载PIP 解压缩tar文件 CD到文件的目录 Sudo python2.7 setup.py install

另一种可能的方法是使用conda和pip。有时你可能只想使用其中的一个,但如果你真的需要设置一个特定版本的python,我会将两者结合起来。

I create a starting conda enviroment with the python I want. As in here https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html. Alternatively you could set up the whole enviroment just using conda. conda create -n myenv python=3.6.4 Then activate your enviroment with the python you like. This command could change depending on the OS. source activae myenv Now you have your python active then you could continue using conda but if you need/want to use pip: python -m pip -r requirements.txt

这里有一种可能的方法。

以下是我对这个问题的看法。适用于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"

在Windows上,如果您选择在Python 3设置期间安装pip模块,则可以通过Python启动器py.exe使用给定的Python版本执行pip模块。

py -3 -m pip install packagename
py -2 -m pip install packagename

你可以更具体地请求Python的精确子版本:

py -3.6 -m pip install packagename

要获得通过启动程序可用的所有已安装Python版本的列表,请运行:

py --list

或者,您可以直接启动所需的Python可执行文件:

C:/path/to/specific/python.exe -m pip install packagename