如何在不更改任何默认值的情况下在 Ubuntu 上为 python 3.8 安装 pip?
How do I install pip for python 3.8 on Ubuntu without changing any defaults?
我正在尝试在 Ubuntu 18.04 LTS
上为 Python 3.8
安装 pip
。
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
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.8
使用 update-alternatives
:
将 python
命令从 python2
更改为 python3.8
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
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
这样的包,所以我不能像这样安装它:
sudo apt install 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
?
虽然我们可以直接使用pip
作为Pythonmodule
(推荐方式):
python -m pip --version
我是这样安装的(所以可以直接调用):
首先,确保命令 pip
可用并且 pip
没有使用它来实现 Python 2.7
sudo apt remove python-pip
现在,如果你在终端中输入 pip
,你会发现那里没有安装任何东西:
pip --version
输出:
Command 'pip' not found, but can be installed with:
sudo apt install python-pip
安装 python3.8
并使用 update-alternatives
在 python
命令上设置正确的版本(如问题中所做的那样)。
请确保您已安装 python3-pip
:
(如果没有 [=29=,这将无法工作。虽然这将安装 pip 9.0.1 from python 3.6
,但我们需要它。)
sudo apt install python3-pip
这会将 pip 9.0.1
安装为 pip3
:
pip3 --version
输出:
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
现在,要为 Python 3.8
安装 pip
,我将 pip
称为 python module
(讽刺!):
python -m pip install pip
输出:
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
时,它确实有效。
现在,确保在 PATH
环境变量中设置了 ~/.local/bin
目录:
使用您喜欢的编辑器打开 ~/.bashrc
(如果您使用的是 zsh
,请将 .bashrc
替换为 .zshrc
)
nano ~/.bashrc
并在文件末尾粘贴以下内容
# 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
(或重新启动终端 window):
source ~/.bashrc
现在,如果您直接尝试 运行 pip
,它将为您提供正确的版本:
pip --version
输出:
pip 20.2 from /home/qumber/.local/lib/python3.8/site-packages/pip (python 3.8)
好甜!
另一种解决方案是安装 apt 中的 pip。 sudo apt install python3-pip
。它安装的 pip 版本适用于 Python 的所有版本,不仅适用于 3.6 版本,一旦安装,您只需使用命令 python3.8 -m pip install pip
更新 pip,他将安装最新版本的 pip Python.
我不建议您删除 Python2,因为它是系统的一个重要模块,您应该在 .bashrc
中为 Python3 创建一个永久的“别名”我喜欢这个 alias python="python3.8
.
几天前我做了这个,我为此付出了很多努力,但我终于成功了,所以我 wrote up what I did as a blog post.
最后我想我可能做了与上述答案大致相同的事情,但如果你跟着它迷路了,也许我的截图等会有所帮助。
这是我完成的过程的 tl;dr:
- 使用
apt
卸载 python3-pip
& python-pip
- 从
/usr/local/bin
中删除旧 pip
文件
- 使用
apt
重新安装 python3-pip
- 将
$HOME/.local/bin
添加到您的 $PATH
(同时重新启动您的 shell 以确保您做对了)
按照 official documentation 中的建议,您可以尝试使用 get-pip.py。
wget https://bootstrap.pypa.io/get-pip.py
python3.8 get-pip.py
这会将 pip 安装为 pip3.8
# install py3.8 and dependencies for the pip3 bootstrap script
add-apt-repository -y ppa:deadsnakes/ppa && \
apt install -y python3.8 python3.8-distutils
# download and run the pip3 bootstrap script
cd /tmp && wget https://bootstrap.pypa.io/get-pip.py && \
python3.8 /tmp/get-pip.py
# use pip py3.8 module to install python packages
python3.8 -m pip install numpy pandas
我正在尝试在 Ubuntu 18.04 LTS
上为 Python 3.8
安装 pip
。
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 from3.6
to3.8
. You SHOULDN'T!
到目前为止,我已经能够使用 PPA
- ppa:deadsnakes/ppa
:
python3.8
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.8
使用 update-alternatives
:
python
命令从 python2
更改为 python3.8
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
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
这样的包,所以我不能像这样安装它:
sudo apt install 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
?
虽然我们可以直接使用pip
作为Pythonmodule
(推荐方式):
python -m pip --version
我是这样安装的(所以可以直接调用):
首先,确保命令 pip
可用并且 pip
没有使用它来实现 Python 2.7
sudo apt remove python-pip
现在,如果你在终端中输入 pip
,你会发现那里没有安装任何东西:
pip --version
输出:
Command 'pip' not found, but can be installed with:
sudo apt install python-pip
安装 python3.8
并使用 update-alternatives
在 python
命令上设置正确的版本(如问题中所做的那样)。
请确保您已安装 python3-pip
:
(如果没有 [=29=,这将无法工作。虽然这将安装 pip 9.0.1 from python 3.6
,但我们需要它。)
sudo apt install python3-pip
这会将 pip 9.0.1
安装为 pip3
:
pip3 --version
输出:
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
现在,要为 Python 3.8
安装 pip
,我将 pip
称为 python module
(讽刺!):
python -m pip install pip
输出:
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
时,它确实有效。
现在,确保在 PATH
环境变量中设置了 ~/.local/bin
目录:
使用您喜欢的编辑器打开 ~/.bashrc
(如果您使用的是 zsh
,请将 .bashrc
替换为 .zshrc
)
nano ~/.bashrc
并在文件末尾粘贴以下内容
# 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
(或重新启动终端 window):
source ~/.bashrc
现在,如果您直接尝试 运行 pip
,它将为您提供正确的版本:
pip --version
输出:
pip 20.2 from /home/qumber/.local/lib/python3.8/site-packages/pip (python 3.8)
好甜!
另一种解决方案是安装 apt 中的 pip。 sudo apt install python3-pip
。它安装的 pip 版本适用于 Python 的所有版本,不仅适用于 3.6 版本,一旦安装,您只需使用命令 python3.8 -m pip install pip
更新 pip,他将安装最新版本的 pip Python.
我不建议您删除 Python2,因为它是系统的一个重要模块,您应该在 .bashrc
中为 Python3 创建一个永久的“别名”我喜欢这个 alias python="python3.8
.
几天前我做了这个,我为此付出了很多努力,但我终于成功了,所以我 wrote up what I did as a blog post.
最后我想我可能做了与上述答案大致相同的事情,但如果你跟着它迷路了,也许我的截图等会有所帮助。
这是我完成的过程的 tl;dr:
- 使用
apt
卸载 - 从
/usr/local/bin
中删除旧 - 使用
apt
重新安装 - 将
$HOME/.local/bin
添加到您的$PATH
(同时重新启动您的 shell 以确保您做对了)
python3-pip
& python-pip
pip
文件
python3-pip
按照 official documentation 中的建议,您可以尝试使用 get-pip.py。
wget https://bootstrap.pypa.io/get-pip.py
python3.8 get-pip.py
这会将 pip 安装为 pip3.8
# install py3.8 and dependencies for the pip3 bootstrap script
add-apt-repository -y ppa:deadsnakes/ppa && \
apt install -y python3.8 python3.8-distutils
# download and run the pip3 bootstrap script
cd /tmp && wget https://bootstrap.pypa.io/get-pip.py && \
python3.8 /tmp/get-pip.py
# use pip py3.8 module to install python packages
python3.8 -m pip install numpy pandas