处理多个 Python 版本和 PIP?

发布于 2024-09-01 07:50:04 字数 234 浏览 4 评论 0原文

有什么方法可以让pip与多个版本的Python兼容?例如,我想使用 pip 将内容显式安装到我的站点 2.5 安装或我的站点 2.6 安装。

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

而且,是的 - 我知道 virtualenv,但不 - 它不是这个特定问题的解决方案。

Is there any way to make pip play well with multiple versions of Python? For example, I want to use pip to explicitly install things to either my site 2.5 installation or my site 2.6 installation.

For example, with easy_install, I use easy_install-2.{5,6}.

And, yes — I know about virtualenv, and no — it's not a solution to this particular problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(29

悲喜皆因你 2024-09-08 07:50:05

pip 也是一个 python 包。因此,将模块安装到特定 python 版本的最简单方法如下

 python2.7 /usr/bin/pip install foo

python2.7 -m pip install foo

pip is also a python package. So the easiest way to install modules to a specific python version would be below

 python2.7 /usr/bin/pip install foo

or

python2.7 -m pip install foo
鸠魁 2024-09-08 07:50:05

显然,easy_installpip有多个版本。看来是大乱子了。无论如何,这就是我在 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
>>> 

So apparently there are multiple versions of easy_install and pip. It seems to be a big mess. Anyway, this is what I did to install Django for Python 2.7 on Ubuntu 12.10:

$ 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
>>> 
探春 2024-09-08 07:50:05

在 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

On Linux, Mac OS X and other POSIX systems, use the versioned Python commands in combination with the -m switch to run the appropriate copy of pip:

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

(appropriately versioned pip commands may also be available)

On Windows, use the py Python launcher in combination with the -m switch:

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

if you get an error for py -3.4 then try:

pip install SomePackage
只怪假的太真实 2024-09-08 07:50:05

安装多个版本的 Python 和各自的软件包。

同一 Windows 计算机上的 Python 版本:2.7、3.4 和 3.6

安装所有 3 个版本的 Python

  • 安装了 Python 2.7、 3.4 和 3.6 具有以下路径

在此处输入图像描述

所有 3 个版本的 Python 的 PATH

  • 确保 PATH 变量(在系统变量中)包含以下路径 - C:\Python27\; C:\Python27\Scripts;C:\Python34\;C:\Python34\Scripts;C:\Python36\;C:\Python36\Scripts\;

重命名版本的可执行文件

  • 将 C:\Python36 和 C:\Python34 中的 python 可执行文件名称分别更改为 python36 和 python34。

输入图像描述这里

检查所有版本的命令提示符:

在此处输入图像描述

为每个版本单独安装软件包

在此处输入图像描述

Installation of multiple versions of Python and respective Packages.

Python version on the same windows machine : 2.7 , 3.4 and 3.6

Installation of all 3 versions of Python :

  • Installed the Python 2.7 , 3.4 and 3.6 with the below paths

enter image description here

PATH for all 3 versions of Python :

  • Made sure the PATH variable ( in System Variables ) has below paths included - C:\Python27\;C:\Python27\Scripts;C:\Python34\;C:\Python34\Scripts;C:\Python36\;C:\Python36\Scripts\;

Renaming the executables for versions :

  • Changed the python executable name in C:\Python36 and C:\Python34 to python36 and python34 respectively.

enter image description here

Checked for the command prompt with all versions :

enter image description here

Installing the packages separately for each version

enter image description here

眼泪淡了忧伤 2024-09-08 07:50:05

如果您有多个版本以及多个体系结构(32 位、64 位),则需要在版本末尾添加 -32 或 -64。

对于 Windows,请转到 cmd 并输入 py --list,它将生成您已安装的版本。该列表将如下所示:

Installed Pythons found by py Launcher for Windows
 -3.7-64 *
 -3.7-32
 -3.6-32

作为示例的完整命令将是:

py -3.6-32 -m pip install (package)

如果您想更深入地了解,要在特定版本的 python 上安装特定版本的包,请在包后面使用 ==(version)。举个例子,

py -3.6-32 -m pip install opencv-python==4.1.0.25

If you have multiple versions as well as multiple architectures (32 bit, 64 bit) you will need to add a -32 or -64 at the end of your version.

For windows, go to cmd and type py --list and it will produce the versions you have installed. The list will look like the following:

Installed Pythons found by py Launcher for Windows
 -3.7-64 *
 -3.7-32
 -3.6-32

The full command as an example will be:

py -3.6-32 -m pip install (package)

If you want to get more indepth, to install a specific version of a package on a specific version of python, use ==(version) after the package. As an example,

py -3.6-32 -m pip install opencv-python==4.1.0.25
温柔戏命师 2024-09-08 07:50:05

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

步骤如下:

  1. 如果您以其他方式安装了多个额外的 python 版本,请删除它们,例如,删除 $HOME/.local/lib/python3.x 等(也是全局安装的)。不过,请勿触及系统的默认 python3 版本。

  2. 在以下目录结构下下载不同Python版本的源代码:

    <前><代码> $HOME/
    python_versions/ :在此处下载 Python-*.tgz 包并“tar xvf”它们。你会得到这样的目录:
    Python-3.4.8/
    Python-3.6.5/
    Python-3.xy/
    ...

  3. 在每个“Python-3.xy/”目录中,执行以下操作(请勿在任何步骤中使用“sudo”! ):

     mkdir root
     ./configure --prefix=$PWD/root 
     使-j 2
     进行安装
     virtualenv --no-site-packages -p root/bin/python3.x env
    
  4. 在“python_versions/”中创建如下文件:

    <前><代码> env_python3x.bash:

    #!/bin/bash
    echo“输入 deactivate 退出”
    源 $HOME/python_versions/Python-3.xy/env/bin/activate

  5. 现在,无论何时您希望选择 python3.x,都可以这样做

     源 $HOME/python_versions/env_python3x.bash
    

可以输入 virtualenv

  1. 在 virtualenv 中,安装你最喜欢的 python 包

     pip install --upgrade package_name
    
  2. 要退出 virtualenv 和 python 版本,只需键入“deactivate”

UPDATE

似乎 --no-site-packages 已被弃用。有一个简单的修复方法:激活 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=< /code>, 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”,神奇的事情就会发生

UPDATE 3 / DOCKER

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

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

Here is my take on the problem. Works for Python3. The main features are:

  • Each Python version is compiled from source
  • All versions are installed locally
  • Does not mangle your system's default Python installation in any way
  • Each Python version is isolated with virtualenv

Prerequisites: If you are using some bare-bones thin client with no extra turf installed, you should run this first (in ubuntu 18.04 at least, extra packages added for convenience):

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 

The steps are as follows:

  1. 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.

  2. 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/
           ...
    
  3. 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
    
  4. 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
    
  5. Now, anytime you wish to opt for python3.x, do

     source $HOME/python_versions/env_python3x.bash
    

to enter the virtualenv

  1. While in the virtualenv, install your favorite python packages with

     pip install --upgrade package_name
    
  2. To exit the virtualenv and python version just type "deactivate"

UPDATE

It seems that --no-site-packages is deprecated. There's an easy fix for this: Once you have activated the virtualenv, just point the HOME env variable to somewhere else than your actual home directory, i.e.:

export HOME=some/where/else

A nice way to do this in general is:

  • Create virtualenv
  • Activate virtualenv
  • If you want to "recycle" existing libraries to your virtualenv, softlink them from your existing install, i.e.
    ln -s $HOME/.local/lib/python3.6/site-packages/numpy $PWD/venv/lib/python3.6/site-packages/
  • Do export PYTHONPATH=, export HOME=/some/other/dir

Now you should have custom-isolated virtualenv.

UPDATE 2 / SUDO

Wan't to force sudo to use your 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"

Now try "sudo python3 --version" and magic should happen

UPDATE 3 / DOCKER

Enable virtualenv inside your docker (of course, you have built it in your docker image):

ENV VIRTUAL_ENV=/home/USER/Python-3.x.y/env
ENV PYTHONPATH=
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
一杆小烟枪 2024-09-08 07:50:05

您可以使用以下命令之一:

pip2 install SomePackage
pip3 install SomePackage

python2 -m pip install SomePackage  
python3 -m pip install SomePackage 

当然,请确保您安装了正确版本的 pip

sudo apt-get install python-pip
sudo apt-get install python3-pip

我自己没有使用过这些命令,但是上面的一些答案建议使用它们来准确指定您要使用的 python 版本

pip-2.7 install SomePackage
python-3.6 -m pip install SomePackage

You can use one of the following commands:

pip2 install SomePackage
pip3 install SomePackage

python2 -m pip install SomePackage  
python3 -m pip install SomePackage 

And of course, make sure that you have the correct version of pip installed

sudo apt-get install python-pip
sudo apt-get install python3-pip

I haven't used these commands myself but, some answers above suggests using them to specify exactly the version of python you want to use

pip-2.7 install SomePackage
python-3.6 -m pip install SomePackage
娇柔作态 2024-09-08 07:50:05

对于 python 3 和 Windows 操作系统,我总是使用此语法在不同版本上安装包:

首先,我总是使用 Git Bash 命令提示符。

这是安装 urllib 包的示例。

默认Python版本:(普通pip命令)

pip install urllib3

对于其他版本

py -3.8 -m pip install urllib3

py =>对于Python

-3.8 =>对于版本(我使用的是 3.8.7 版本),但如果您使用的是 3.7.7 版本,它将是“-3.7”

-m :只是因为或为了修改

pip install urllib3:普通的 pip 命令

For python 3 and Windows OS, I always use this syntax to install packages on different version:

First I always use Git Bash Command Prompt.

Here an example installing urllib package.

Default Python version:(The normal pip command)

pip install urllib3

For the other versions

py -3.8 -m pip install urllib3

py => for python

-3.8 => for the version (I'm using the 3.8.7 version) but if you're using the 3.7.7 version it will be "-3.7"

-m : just because or for modify

pip install urllib3 : the normal pip command

浮萍、无处依 2024-09-08 07:50:05

22.3,您可以使用--python 选项允许 pip 管理除安装 pip 之外的 Python 环境。

python -m pip --python <PATH_TO_PYTHON_INTERPRETER_YOU_WANT_TO_MANAGE> install package_name

引用文档中的

有时,您可能想使用 pip 来管理 Python
安装除 pip 之外的其他安装。在这种情况下,
您可以使用 --python 选项来指定您想要的解释器
来管理。此选项可以采用两个值之一:

  1. Python 可执行文件的路径。
  2. 虚拟环境的路径。

在这两种情况下,pip 的运行方式与从
Python 环境。

这可能有用的一个例子是管理虚拟
未安装 pip 的环境。

$ python -m venv .venv --without-pip
$ python -m pip --python .venv install SomePackage
[...]
成功安装 SomePackage

您还可以使用--python .venv/bin/python(或者在Windows上,
--python .venv\Scripts\python.exe) 如果你想明确,但是
虚拟环境名称更短并且工作原理完全相同。

Starting from 22.3, you can use --python option to allow pip to manage Python environments other than the one pip is installed in.

python -m pip --python <PATH_TO_PYTHON_INTERPRETER_YOU_WANT_TO_MANAGE> install package_name

Quoting from the documentation:

Occasionally, you may want to use pip to manage a Python
installation other than the one pip is installed into. In this case,
you can use the --python option to specify the interpreter you want
to manage. This option can take one of two values:

  1. The path to a Python executable.
  2. The path to a virtual environment.

In both cases, pip will run exactly as if it had been invoked from
that Python environment.

One example of where this might be useful is to manage a virtual
environment that does not have pip installed.

$ python -m venv .venv --without-pip
$ python -m pip --python .venv install SomePackage
[...]
Successfully installed SomePackage

You could also use --python .venv/bin/python (or on Windows,
--python .venv\Scripts\python.exe) if you wanted to be explicit, but
the virtual environment name is shorter and works exactly the same.

不疑不惑不回忆 2024-09-08 07:50:05

这里的大多数答案都解决了这个问题,但我想添加一些关于在 CentOS 7 上的 /usr/local 中创建 python 替代安装时一直让我困惑的东西。当我在那里安装时,看起来 pip 正在工作,因为我可以使用 pip2.7 install 并且它会安装模块。然而,我不明白的是为什么我新安装的 python 版本看不到我正在安装的内容。

原来在CentOS 7中,/usr/bin文件夹下已经有一个python2.7和一个pip2.7。要为新的 python 发行版安装 pip,您需要明确告诉 sudo 转到 /usr/local/bin

sudo /usr/local/bin/python2.7 -m ensurepip

这应该将 pip2.7 安装在您的 /usr/local/bin< /code> 文件夹以及您的 python 版本。诀窍是,当你想安装模块时,你要么需要修改 sudo $PATH 变量以包含 /usr/local/bin ,要么需要

sudo /usr/local/bin/pip2.7 install <module>

执行想要安装新模块。我花了很长时间才想起 sudo 并没有立即看到 /usr/local/bin

Most of the answers here address the issue but I want to add something what was continually confusing me with regard to creating an alternate installation of python in the /usr/local on CentOS 7. When I installed there, it appeared like pip was working since I could use pip2.7 install and it would install modules. However, what I couldn't figure out was why my newly installed version of python wasn't seeing what I was installing.

It turns out in CentOS 7 that there is already a python2.7 and a pip2.7 in the /usr/bin folder. To install pip for your new python distribution, you need to specifically tell sudo to go to /usr/local/bin

sudo /usr/local/bin/python2.7 -m ensurepip

This should get pip2.7 installed in your /usr/local/bin folder along with your version of python. The trick is that when you want to install modules, you either need to modify the sudo $PATH variable to include /usr/local/bin or you need to execute

sudo /usr/local/bin/pip2.7 install <module>

if you want to install a new module. It took me forever to remember that sudo wasn't immediately seeing /usr/local/bin.

萌︼了一个春 2024-09-08 07:50:05

上下文:Archlinux

操作:
安装 python2-pip:
sudo pacman -S python2-pip

您现在拥有 pip2.7:
sudo pip2.7 install boto

测试(在我的例子中,我需要“boto”):
运行以下命令:

python2
import boto

成功:没有错误。

退出:Ctrl+D

Context: Archlinux

Action:
Install python2-pip:
sudo pacman -S python2-pip

You now have pip2.7:
sudo pip2.7 install boto

Test (in my case I needed 'boto'):
Run the following commands:

python2
import boto

Success: No error.

Exit: Ctrl+D

心碎无痕… 2024-09-08 07:50:05

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

  1. https://pypi.python.org/pypi/pip (tar)
  2. 解压tar文件
  3. cd 到文件所在目录
  4. sudo python2.7 setup.py install

for example, if you set other versions (e.g. 3.5) as default and want to install pip for python 2.7:

  1. download pip at https://pypi.python.org/pypi/pip (tar)
  2. unzip tar file
  3. cd to the file’s directory
  4. sudo python2.7 setup.py install
Bonjour°[大白 2024-09-08 07:50:05

如果您同时安装了 python3.6python3.7 并且希望将 pippython3.7 一起使用默认情况下,您应该执行以下操作:

首先确保您已为 python3.7 安装了 pip

python3.7 -m pip install -U pip

现在 pip3.7 必须可用,因此我们编辑 .bashrc

nano ~/.bashrc

添加以下行为

alias pip=pip3.7

了使更改生效,请在 shell 中输入:

source ~/.bashrc

现在,如果您输入:

pip --version

您应该得到:

pip 20.1.1 来自 /usr/local/lib/python3.7/dist-packages/pip (python 3.7)

这意味着,如果您使用,例如:

pip install <package>

它将安装 对于python3.7

If you have both python3.6 and python3.7 installed and want to use pip with python3.7 by default, here's what you should do:

First make sure you have pip installed for python3.7

python3.7 -m pip install -U pip

Now pip3.7 must be available, so we edit .bashrc

nano ~/.bashrc

adding the following line to it

alias pip=pip3.7

In order for the changes to take effect type in the shell:

source ~/.bashrc

Now if you type:

pip --version

you should get:

pip 20.1.1 from /usr/local/lib/python3.7/dist-packages/pip (python 3.7)

which means, if you use, for example:

pip install <package>

it would install the <package> for python3.7

祁梦 2024-09-08 07:50:05

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

  1. 我用我想要的 python 创建了一个起始 conda 环境。就像这里 https://docs .conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html。或者,您可以仅使用 conda 设置整个环境。

    conda create -n myenv python=3.6.4

  2. 然后使用您喜欢的 python 激活您的环境。此命令可能会根据操作系统而变化。

    源 activae myenv

  3. 现在你的 python 已处于活动状态,那么你可以继续使用 conda,但如果你需要/想要使用 pip:

    python -m pip -r requests.txt

这里你有一个可能的方法。

Another possible way could be using conda and pip. Some time you probably want to use just one of those, but if you really need to set up a particular version of python I combine both.

  1. 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

  2. Then activate your enviroment with the python you like. This command could change depending on the OS.

    source activae myenv

  3. 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

Here you have a possible way.

别想她 2024-09-08 07:50:05

要使用多个版本的 pip,只需键入

pip{ve​​rsion} 并运行命令

示例:对于 python 3.10

pip3.10

<块引用>

pip3.10列表

pip3.10

适用于 Python 3.7

pip3.7

<块引用>

pip3.7列表

To use multiple versions of pip, just type

pip{version} and run command

Example: for python 3.10

pip3.10

pip3.10 list

pip3.10

for Python 3.7

pip3.7

pip3.7 list

雪花飘飘的天空 2024-09-08 07:50:05

在使用 pip 的特定位置时更新 PIP 并安装软件包:

在某些设备上使用简单的命令行 pip install 时,我收到间歇性错误“访问被拒绝”所有用户使用 c:\users\... 文件夹而不是 c:\Program Files\Python 文件夹中的 pip 的实验室计算机。
感谢上面的 MultiplyByZer0 答案,我能够实现一个指定 python 路径然后运行 ​​pip 的解决方案。

这是我用来强制 pip 更新然后安装 python 库的 powershell 脚本:

$fullPath = "C:\Program Files\Python38"
$thisApp = "python.exe"
$arguments = "-m pip install --upgrade pip"
$appPath = join-path $fullPath $thisApp
Start-Process $appPath $arguments  -Verb RunAs -Wait
$packages = @(  "pylint"
                "pillow"
                "pygame"
                "matplotlib"
                "pandas"
                "godirect")
foreach ($package in $packages)
   {
       Write-Host "Installing python Package" $package -ForegroundColor Yellow
       $arguments = "-m pip install $package"
       Start-Process $appPath $arguments  -Verb RunAs -Wait
   }

Updating PIP and installing Packages while using specific location of pip:

I was getting an intermittent error "Access Denied" when using the simple comand line pip install <library> on some lab machines that were using pip from a c:\users\... folder instead of the c:\Program Files\Python folder for all users.
Thanks to MultiplyByZer0 answer above I was able to implement a solution that specified the path to python and then run pip.

Here is the powershell script I use to force pip update and then install python libraries:

$fullPath = "C:\Program Files\Python38"
$thisApp = "python.exe"
$arguments = "-m pip install --upgrade pip"
$appPath = join-path $fullPath $thisApp
Start-Process $appPath $arguments  -Verb RunAs -Wait
$packages = @(  "pylint"
                "pillow"
                "pygame"
                "matplotlib"
                "pandas"
                "godirect")
foreach ($package in $packages)
   {
       Write-Host "Installing python Package" $package -ForegroundColor Yellow
       $arguments = "-m pip install $package"
       Start-Process $appPath $arguments  -Verb RunAs -Wait
   }
活泼老夫 2024-09-08 07:50:05

根据最新的 Ubuntu-LTS 22.04 和 python 3 进行更新,但对于 python 2 可以遵循类似的说明并进行适当的版本更改。

您可以考虑使用 pyenv python 环境管理器,它可以让您并行配置不同版本的 python,而无需创建单独的 virtualenv 或需要安装单独的 pip。

安装说明可以在这里找到,配置 pyenv 非常容易。 https://github.com/pyenv/pyenv#readme

但是,这确实需要您安装可以按如下方式安装必备库:

基于 Redhat 的系统

sudo yum install git gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel make curl llvm

基于 Ubuntu/Debian 的 linux 系统

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev 
 libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl git

安装必备库后,请按照 README.md 中的说明进行操作
安装完成后,要检查系统中存在的 python 版本,请在终端中运行以下命令。

pyenv versions

注意,如果您在设置 pyenv 之前安装了 python 版本,它可能无法检测到已安装的版本,您可能必须删除它们(包括依赖项)。执行此操作时请注意不要删除重要的 lib 文件。

安装新版本的 python

pyenv install <version_number> # like 3.9 for installing python3.9

安装后将 python 版本设置为默认版本,运行以下命令。

pyenv global <version_number> # to set it globally
pyenv local <version_number> # to set it in the local directory

您可以随时使用 pyenv globalpyenv localpyenv shell 命令以及所需的版本号在已安装的 Python 版本之间进行切换。

请注意,这种安排与虚拟环境不同,因为它允许您在使用其他系统级库和包的同时在系统范围内管理多个版本的 python。然而 pyenv 确实有插件来启用 virtualenv 支持,可以按照此处列出的说明进行安装 https://github .com/pyenv/pyenv-virtualenv#readme

只需删除 pyenv 文件夹并恢复对 .bashrc.bash_profile 和/或类似的文件。

希望这有帮助。

Updated according to latest Ubuntu-LTS 22.04 and for python 3, however similar instructions can be followed for python 2 with appropriate version changes.

You can consider pyenv python environment manager which lets you configure different versions of python parallely without needing to create a separate virtualenv or requiring separate pip installed.

The installation instructions can be found here and it is pretty easy to configure pyenv. https://github.com/pyenv/pyenv#readme

This does however require you to install prerequisite libraries which can be installed as follows:

Redhat based systems

sudo yum install git gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel make curl llvm

Ubuntu/Debian based linux systems

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev 
 libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl git

After installing prerequisites follow the instructions in the README.md
Once installation is finished, to check python versions present in your system run the following in your terminal

pyenv versions

Note if you have installed python versions before setting up pyenv it may not detect the installed versions and you might have to remove them including dependencies. Take care to not remove important lib files while doing so.

To install a new version of python

pyenv install <version_number> # like 3.9 for installing python3.9

Once installed to set the python version as default run the following.

pyenv global <version_number> # to set it globally
pyenv local <version_number> # to set it in the local directory

You can switch between installed Python versions at any time using the pyenv global, pyenv local, or pyenv shell commands with the desired version number.

Note that this arrangement is different from a virtual environment as it allows you to manage multiple versions of python at a system-wide level while using other system level libraries and packages. However pyenv does have plugins to enable virtualenv support which can be installed from instructions listed here https://github.com/pyenv/pyenv-virtualenv#readme

Pyenv can be removed by simply deleting the pyenv folder and reverting any changes made to .bashrc, .bash_profile and/or similar files.

Hope this helps.

寒江雪… 2024-09-08 07:50:05

例如,您可以转到 C:\Python2.7\Scripts,然后从该路径运行 cmd。之后你可以运行 pip2.7 install yourpackage...

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

You can go to for example C:\Python2.7\Scripts and then run cmd from that path. After that you can run pip2.7 install yourpackage...

That will install package for that version of Python.

小清晰的声音 2024-09-08 07:50:05

这可能是完全错误的做法(我是一个 python 菜鸟),但我只是进去编辑了 pip 文件

#!/usr/bin/env python3 <-- I changed this line.

# -*- coding: utf-8 -*-
import re
import sys

from pip._internal import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?
, '', sys.argv[0])
    sys.exit(main())

This is probably the completely wrong thing to do (I'm a python noob), but I just went in and edited the pip file

#!/usr/bin/env python3 <-- I changed this line.

# -*- coding: utf-8 -*-
import re
import sys

from pip._internal import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?
, '', sys.argv[0])
    sys.exit(main())
空宴 2024-09-08 07:50:05

特别是对于 Windows:
\path\to\python.exe -m pip install PackageName 有效。

For windows specifically:
\path\to\python.exe -m pip install PackageName works.

汹涌人海 2024-09-08 07:50:05

对于搅拌机:

/usr/bin $ python3.7 -m pip install irc

for Blender:

/usr/bin $ python3.7 -m pip install irc
孤云独去闲 2024-09-08 07:50:04

当前建议是使用python -m pip,其中 python 是您要使用的 Python 版本。这是建议,因为它适用于所有版本的 Python 和所有形式的 virtualenv。例如:

# The system default python:
$ python -m pip install fish

# A virtualenv's python:
$ .env/bin/python -m pip install fish

# A specific version of python:
$ python-3.6 -m pip install fish

之前的答案,留给后人:

从0.8版本开始,Pip支持pip-{version}。您可以像 easy_install-{version} 一样使用它:

$ pip-2.5 install myfoopackage
$ pip-2.6 install otherpackage
$ pip-2.7 install mybarpackage

编辑:pip 更改了其架构以使用 pipVERSION 而不是 pip-版本 1.5 中的版本。如果您有 pip >= 1.5,您应该使用以下内容:

$ pip2.6 install otherpackage
$ pip2.7 install mybarpackage

检查 https ://github.com/pypa/pip/pull/1053 了解更多详细信息


参考:

The current recommendation is to use python -m pip, where python is the version of Python you would like to use. This is the recommendation because it works across all versions of Python, and in all forms of virtualenv. For example:

# The system default python:
$ python -m pip install fish

# A virtualenv's python:
$ .env/bin/python -m pip install fish

# A specific version of python:
$ python-3.6 -m pip install fish

Previous answer, left for posterity:

Since version 0.8, Pip supports pip-{version}. You can use it the same as easy_install-{version}:

$ pip-2.5 install myfoopackage
$ pip-2.6 install otherpackage
$ pip-2.7 install mybarpackage

EDIT: pip changed its schema to use pipVERSION instead of pip-VERSION in version 1.5. You should use the following if you have pip >= 1.5:

$ pip2.6 install otherpackage
$ pip2.7 install mybarpackage

Check https://github.com/pypa/pip/pull/1053 for more details


References:

夜访吸血鬼 2024-09-08 07:50:04

在 Windows 上,您可以通过 Python 启动器使用给定的 Python 版本执行 pip 模块py.exe(如果您在 Python 3 安装过程中选择安装它)。

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

On Windows, you can execute the pip module using a given Python version through the Python launcher, py.exe, if you chose to install it during Python 3 setup.

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

You can be even more specific and request an exact sub-version of Python:

py -3.6 -m pip install packagename

To get a list of all installed Python versions available through the launcher, run:

py --list

Alternatively, you can launch the desired Python executable directly:

C:/path/to/specific/python.exe -m pip install packagename
烧了回忆取暖 2024-09-08 07:50:04

/path/to/python2.{5,6} /path/to/pip install PackageName 不起作用?

要使其在尚未安装 pip 的任何 python 版本上运行,您需要下载 pip 并执行 python*version* setup.py install。例如python3.3 setup.py install。这解决了注释中的导入错误。 (根据@hbdgaf的建议)

/path/to/python2.{5,6} /path/to/pip install PackageName doesn't work?

For this to work on any python version that doesn't have pip already installed you need to download pip and do python*version* setup.py install. For example python3.3 setup.py install. This resolves the import error in the comments. (As suggested by @hbdgaf)

街道布景 2024-09-08 07:50:04

我默认安装了 python 2.6 (Amazon EC2 AMI),但我的应用程序需要 python2.7 和一些外部包。假设你已经安装了 python2.7 和默认的 python(在我的例子中是 2.6)。以下是如何为非默认 python2.7 安装 pip 和软件包

为您的 python 版本安装 pip:

curl -O https://bootstrap.pypa.io/get-pip.py
python27 get-pip.py

使用特定的 pip 版本安装软件包:

pip2.7 install mysql-connector-python --allow-external mysql-connector-python

I had python 2.6 installed by default (Amazon EC2 AMI), but needed python2.7 plus some external packages for my application. Assuming you already installed python2.7 alongside with default python (2.6 in my case). Here is how to install pip and packages for non-default python2.7

Install pip for your python version:

curl -O https://bootstrap.pypa.io/get-pip.py
python27 get-pip.py

Use specific pip version to install packages:

pip2.7 install mysql-connector-python --allow-external mysql-connector-python
笑红尘 2024-09-08 07:50:04

它在 Windows 中以这种方式为我工作:

  1. 我将 python 文件 python.py 和 pythonw.exe 的名称更改为 python3.py pythonw3.py

  2. 然后我只是在提示符中运行此命令:

    python3 -m pip 安装包

It worked for me in windows this way:

  1. I changed the name of python files python.py and pythonw.exe to python3.py pythonw3.py

  2. Then I just ran this command in the prompt:

    python3 -m pip install package

凡间太子 2024-09-08 07:50:04

其他答案显示了如何在 2.X 和 3.X Python 中使用 pip,但没有显示如何处理多个 Python 发行版(例如原始 Python 和 Anaconda Python)的情况

我总共有3个Python版本:原始Python 2.7和Python 3.5以及Anaconda Python 3.5。

以下是我如何将包安装到:

  1. 原始Python 3.5

    /usr/bin/python3 -m pip install python-daemon
    
  2. 原始Python 2.7

    /usr/bin/python -m pip install python-daemon
    
  3. Anaconda Python 3.5

    python3 -m pip install python-daemon
    

    pip3 安装 python-daemon
    

    更简单,因为 Anaconda 会覆盖用户环境中的原始 Python 二进制文件。

    当然,在anaconda中安装应该使用conda命令完成,这只是一个示例。


另外,请确保为该特定 python 安装了 pip。您可能需要手动安装 pip。这适用于 Ubuntu 16.04:

sudo apt-get install python-pip 

sudo apt-get install python3-pip

Other answers show how to use pip with both 2.X and 3.X Python, but does not show how to handle the case of multiple Python distributions (eg. original Python and Anaconda Python).

I have a total of 3 Python versions: original Python 2.7 and Python 3.5 and Anaconda Python 3.5.

Here is how I install a package into:

  1. Original Python 3.5:

    /usr/bin/python3 -m pip install python-daemon
    
  2. Original Python 2.7:

    /usr/bin/python -m pip install python-daemon
    
  3. Anaconda Python 3.5:

    python3 -m pip install python-daemon
    

    or

    pip3 install python-daemon
    

    Simpler, as Anaconda overrides original Python binaries in user environment.

    Of course, installing in anaconda should be done with conda command, this is just an example.


Also, make sure that pip is installed for that specific python.You might need to manually install pip. This works in Ubuntu 16.04:

sudo apt-get install python-pip 

or

sudo apt-get install python3-pip
謸气贵蔟 2024-09-08 07:50:04

从这里: 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

From here: https://docs.python.org/3/installing/

Here is how to install packages for various versions that are installed at the same time 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

On Windows, use the py Python launcher in combination with the -m switch:

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
棒棒糖 2024-09-08 07:50:04

我最近遇到了这个问题,发现我在也有 Python 2 的 Linux 系统上没有获得适用于 Python 3 的正确 pip。

首先,您必须确保已安装适用于您的 python 版本的 pip:

对于 Python 2:

sudo apt-get install python-pip

对于 Python 3:

sudo apt-get install python3-pip

然后要安装一个版本的 Python 或另一个版本的软件包,只需对 Python 2 使用以下命令:

pip install <package>

或对 Python 3 使用以下命令:

pip3 install <package>

I ran into this issue myself recently and found that I wasn't getting the right pip for Python 3, on my Linux system that also has Python 2.

First you must ensure that you have installed pip for your python version:

For Python 2:

sudo apt-get install python-pip

For Python 3:

sudo apt-get install python3-pip

Then to install packages for one version of Python or the other, simply use the following for Python 2:

pip install <package>

or for Python 3:

pip3 install <package>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文