如何在Ubuntu 16.10上为Python 3.6安装pip?

首先,我想指出这个问题可能看起来像是重复的,但实际上并不是。我在Ask Ubuntu上看到的所有问题都涉及Python 3的pip,而我谈论的是Python 3.6。那时使用的步骤对于Python 3.6不起作用。

  1. 我从官方Docker商店获取了一个清晰的Ubuntu 16.10映像。
  2. 运行apt-get update
  3. 运行apt-get install python3.6
  4. 运行apt-get install python3-pip
  5. 运行pip3 install requests bs4
  6. 运行python3.6 script.py

下面出现了ModuleNotFoundError

 Traceback (most recent call last):
    File "script.py", line 6, in <module>
     import requests
 ModuleNotFoundError: No module named 'requests'

我机器上安装了Python和pip。
python3
python3.5
python3.5m
python3.6
python3m
python3-config
python3.5-config
python3.5m-config
python3.6m
python3m-config  

pip
pip3
pip3.5

问题已经在Stack Overflow上重复发布 - wjandrea
2个回答

这个答案假设您已经安装了python3.6。对于python3.7,请将3.6替换为3.7。对于python3.8,请将3.6替换为3.8,但可能还需要先安装python3.8-distutils软件包。
使用sudo进行安装
关于安装pip,使用curl(而不是wget)可以避免将文件写入磁盘。
curl https://bootstrap.pypa.io/get-pip.py | sudo -H python3.6

显然,在安装更新的Python解释器时,使用sudo命令时,需要使用-H标志以防止出现以下错误:

目录'/home/someuser/.cache/pip/http'或其父目录不属于当前用户,并且缓存已被禁用。请检查该目录的权限和所有者。如果使用sudo执行pip命令,则可能需要使用sudo的-H标志。

目录'/home/someuser/.cache/pip'或其父目录不属于当前用户,并且缓存轮子已被禁用。请检查该目录的权限和所有者。如果使用sudo执行pip命令,则可能需要使用sudo的-H标志。

无需sudo进行安装

curl https://bootstrap.pypa.io/get-pip.py | python3.6 - --user

有时候会出现以下警告信息:
WARNING: The script wheel is installed in '/home/ubuntu/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
验证
完成后,可以预期pippip3pip3.6都指向同一个目标。
$ (pip -V && pip3 -V && pip3.6 -V) | uniq
pip 18.0 from /usr/local/lib/python3.6/dist-packages (python 3.6)

当然,你也可以选择使用python3.6 -m pip
$ python3.6 -m pip -V
pip 18.0 from /usr/local/lib/python3.6/dist-packages (python 3.6)

6脚本似乎假设python3.6具有可用的setup-tools / easy install。因此,它对我来说无效。最终我使用了curl https://bootstrap.pypa.io/ez_setup.py -o - | python3.6 && python3.6 -m easy_install pip - FirefoxMetzger
我在Ubuntu 14.04上从ppa:jonathonf/python-3.6安装了python3.6python3.6-dev。我不确定是我忘记安装setuptools还是它出了问题,但当时它无法正常工作。 - FirefoxMetzger
这并不会以任何方式否定你的答案 =) 这只是一个附加内容,以防万一其他人遇到相同(希望不常见)的问题。 - FirefoxMetzger
如果我想让 pip -V 显示为 python 2.7,而 pip3 -V && pip3.6 -V 显示为 python 3.6,我该怎么做? - Benyamin Jafari
@BenyaminJafari 首先运行 curl https://bootstrap.pypa.io/get-pip.py | sudo python3.6,然后运行 curl https://bootstrap.pypa.io/get-pip.py | sudo python2.7。我没有尝试过,但可能会起作用。 - Asclepius
1你也可以使用wget -O -将结果流式传输到标准输出,例如:wget -O - https://bootstrap.pypa.io/get-pip.py | sudo -H python3.6 - ingomueller.net
curl https://bootstrap.pypa.io/get-pip.py | sudo -H python3.6 是正确的方法。现在我的 napalm 安装在 python3.6 上,而不是 python3.5。 - trogne
看起来对于Python 3.6,命令应该有一点不同,因为建议的命令在我的电脑上出错了:“错误:此脚本不适用于Python 3.6。最低支持的Python版本是3.7。请使用https://bootstrap.pypa.io/pip/3.6/get-pip.py代替。” - php-coder

我在stackoverflow上得到了一个答案。
来源:https://stackoverflow.com/a/44254088/1812319

Let's suppose that you have a system running Ubuntu 16.04, 16.10, or 17.04, and you want Python 3.6 to be the default Python.

If you're using Ubuntu 16.04 LTS, you'll need to use a PPA:

sudo add-apt-repository ppa:jonathonf/python-3.6  # (only for 16.04 LTS)

Then, run the following (this works out-of-the-box on 16.10 and 17.04):

sudo apt update
sudo apt install python3.6
sudo apt install python3.6-dev
sudo apt install python3.6-venv
wget https://bootstrap.pypa.io/get-pip.py
sudo python3.6 get-pip.py
sudo ln -s /usr/bin/python3.6 /usr/local/bin/python3
sudo ln -s /usr/local/bin/pip /usr/local/bin/pip3

# Do this only if you want python3 to be the default Python
# instead of python2 (may be dangerous, esp. before 2020):
# sudo ln -s /usr/bin/python3.6 /usr/local/bin/python

When you have completed all of the above, each of the following shell commands should indicate Python 3.6.1 (or a more recent version of Python 3.6):

python --version   # (this will reflect your choice, see above)
python3 --version
$(head -1 `which pip` | tail -c +3) --version
$(head -1 `which pip3` | tail -c +3) --version

1没有找到pip模块 - Turtles Are Cute
在运行该命令之前,尝试执行sudo apt get updatesudo apt get upgrade命令。 - JChris
非常感谢你的帮助,我非常感激。在解决问题的过程中,我的系统陷入了一个登录循环的状态,看起来最简单的解决方法是重新格式化/重新安装。 - Turtles Are Cute