如何在不更改任何默认值的情况下在 Ubuntu 上为 python 3.8 安装 pip? [英] How do I install pip for python 3.8 on Ubuntu without changing any defaults?

查看:160
本文介绍了如何在不更改任何默认值的情况下在 Ubuntu 上为 python 3.8 安装 pip?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Ubuntu 18.04 LTS 上为 Python 3.8 安装 pip.

I'm trying to install pip for Python 3.8 on an Ubuntu 18.04 LTS.

我知道这已经被问了太多次了.但是这些问题并不特别关注保留 Ubuntu 的默认值.而这些问题的答案要么不起作用,要么继续提出一些如此激烈以至于会破坏系统的东西——例如将默认的 python3 版本从 3.6 更改为 3.8.你不应该

I know this has been asked way too many times. But those questions do not concern keeping Ubuntu's defaults specifically. And the answers on those questions either don't work or go on to suggest something so drastic that it would break the system - e.g. change default python3 version from 3.6 to 3.8. You SHOULDN'T!

到目前为止,我已经能够使用 PPA - ppa:deadsnakes/ppa 成功安装 python3.8:>

So far, I've been able to install python3.8 successfully using the PPA - ppa:deadsnakes/ppa:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.8

使用 update-alternativespython 命令从 python2 更改为 python3.8:

Changed python command from python2 to python3.8 using update-alternatives:

update-alternatives --remove python /usr/bin/python2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 10

现在,当我运行 python --version 时,我得到 python 3.8:

Now, I get python 3.8 when I run python --version:

Python 3.8.5

Python 3.8.5

问题是,我仍然无法为 Python 3.8 安装 pip.
如果我尝试安装 python3-pip,它会为 Python 3.6 安装 pip,因为 python3 仍然指向 python3.6.9,我打算保持这种方式.
尝试安装 python-pip,它会为 Python 2.7 安装 pip.
也没有像 python3.8-pip 这样的包,所以我不能像这样安装它:

The problem is, I still can't install pip for Python 3.8.
If I try to install python3-pip, it installs pip for Python 3.6 since python3 still points to python3.6.9, and I intend to keep it that way.
Try installing python-pip, and it will install pip for Python 2.7.
Also there's no such package as python3.8-pip, so I can't install it like:

sudo apt install python3.8-pip

输出:

E: 无法定位包 python3.8-pip
E: 无法通过 glob 'python3.8-pip' 找到任何包
E: 无法通过正则表达式python3.8-pip"找到任何包

E: Unable to locate package python3.8-pip
E: Couldn't find any package by glob 'python3.8-pip'
E: Couldn't find any package by regex 'python3.8-pip'

如何在 Ubuntu 18.04 上为 Python 3.8 安装 pip?

What can I do to install pip for Python 3.8 on an Ubuntu 18.04?

推荐答案

虽然我们可以直接使用 pip 作为 Python module(推荐的方式):

While we can use pip directly as a Python module (the recommended way):

python -m pip --version

我是这样安装的(所以可以直接调用):
首先,确保命令 pip 可用,并且 pip 没有将其用于 Python 2.7

This is how I installed it (so it can be called directly):
Firstly, make sure that command pip is available and it isn't being used by pip for Python 2.7

sudo apt remove python-pip

现在,如果您在终端中编写 pip,您会发现那里没有安装任何东西:

Now if you write pip in the Terminal, you'll get that nothing is installed there:

pip --version

输出:

找不到命令pip",但可以通过以下方式安装:

Command 'pip' not found, but can be installed with:

sudo apt install python-pip

sudo apt install python-pip

安装 python3.8 并使用 update-alternativespython 命令上设置正确的版本(如问题中所述).

Install python3.8 and setup up correct version on python command using update-alternatives (as done in the question).

确保您已安装 python3-pip:
(如果没有 python3-pip,这将无法工作.虽然这将安装 pip 9.0.1 from python 3.6,但我们需要它.)

Make sure, you have python3-pip installed:
(This won't work without python3-pip. Although this will install pip 9.0.1 from python 3.6, we'll need it.)

sudo apt install python3-pip

这会将 pip 9.0.1 安装为 pip3:

pip3 --version

输出:

来自/usr/lib/python3/dist-packages (python 3.6) 的 pip 9.0.1

pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

现在,要为 Python 3.8 安装 pip,我使用了 pip,将其称为 python 模块(讽刺!):

Now, to install pip for Python 3.8, I used pip by calling it as a python module (ironic!):

python -m pip install pip

输出:

收集点子
下载 pythonhosted.org/packages/36/74/38c2410d688ac7b48afa07d413674afc1f903c1c1f854de51dc8eb2367a5/pip-20.2-py2.py3-none-any.whl (1.5)100% |████████████████████████████████|1.5MB 288kB/秒
安装收集的包:pip
成功安装pip-20.2

Collecting pip
  Downloading https://files.pythonhosted.org/packages/36/74/38c2410d688ac7b48afa07d413674afc1f903c1c1f854de51dc8eb2367a5/pip-20.2-py2.py3-none-any.whl (1.5MB)
  100% |████████████████████████████████| 1.5MB 288kB/s
Installing collected packages: pip
Successfully installed pip-20.2

看起来,当我将 pip(它是为 Python 3.6 安装的,顺便说一句)作为 Python 3.8 的一个模块调用并安装 pip 时,它确实有效.

It looks like, when I called pip (which was installed for Python 3.6, BTW) as a module of Python 3.8, and installed pip, it actually worked.

现在,确保您的 ~/.local/bin 目录设置在 PATH 环境变量中:
使用您喜欢的编辑器打开 ~/.bashrc(如果您使用的是 zsh,请将 .bashrc 替换为 .zshrc>)

Now, make sure your ~/.local/bin directory is set in PATH environment variable:
Open ~/.bashrc using your favourite editor (if you're using zsh, replace .bashrc with .zshrc)

nano ~/.bashrc

并将以下内容粘贴到文件末尾

And paste the following at the end of the file

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

最后,获取您的 .bashrc(或重新启动终端窗口):

Finally, source your .bashrc (or restart the Terminal window):

source ~/.bashrc

现在,如果您尝试直接运行 pip,它将为您提供正确的版本:

Now if you try running pip directly it'll give you the correct version:

pip --version

输出:

来自/home/qumber/.local/lib/python3.8/site-packages/pip (python 3.8) 的 pip 20.2

pip 20.2 from /home/qumber/.local/lib/python3.8/site-packages/pip (python 3.8)

甜!

这篇关于如何在不更改任何默认值的情况下在 Ubuntu 上为 python 3.8 安装 pip?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆