问题描述
首先,我想指出这个问题似乎是重复的,但事实并非如此。我在这里看到的所有问题都是关于Python 3的pip,而我正在谈论的是Python 3.6。那时使用的步骤不适用于Python 3.6。
-
我从official docker store获得了清晰的Ubuntu 16.10映像。
-
运行
apt-get update
-
运行
apt-get install python3.6
-
运行
apt-get install python3-pip
-
运行
pip3 install requests bs4
-
运行
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
最佳方法
假设您有一个运行Ubuntu 16.04、16.10或17.04的系统,并且希望Python 3.6是默认的Python。
如果您使用的是Ubuntu 16.04 LTS,则需要使用PPA:
sudo add-apt-repository ppa:jonathonf/python-3.6 # (only for 16.04 LTS)
然后,运行以下命令(这适用于16.10和17.04上的out-of-the-box):
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
完成上述所有操作后,以下每个Shell命令都应指示Python 3.6.1
(或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
次佳方法
至少在Ubuntu 16.10中,默认的python3
为python3.5
。因此,所有python3-X
软件包都将安装用于python3.5,而不是python3.6。
您可以通过检查pip3
的shebang进行验证:
$ head -n1 $(which pip3)
#!/usr/bin/python3
幸运的是,由python3-pip
软件包安装的pip已安装到”shared” /usr/lib/python3/dist-packages
中,以便python3.6也可以利用它。
您可以通过执行以下操作为python3.6安装软件包:
python3.6 -m pip install ...
例如:
$ python3.6 -m pip install requests
$ python3.6 -c 'import requests; print(requests.__file__)'
/usr/local/lib/python3.6/dist-packages/requests/__init__.py
第三种方法
该答案假定您已安装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
-H
标志对于sudo
显然是必需的,以防止在为更新的python解释器安装pip时出现以下错误:
The directory ‘/home/someuser/.cache/pip/http’ or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo’s -H flag.
The directory ‘/home/someuser/.cache/pip’ or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo’s -H flag.
没有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.
Verification
此后,可以预期pip
,pip3
和pip3.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)
第四种方法
该网站包含一个更干净的解决方案,它使点码完整无缺,as-well可以轻松地在3.5和3.6之间切换,然后在发布3.7时轻松切换。
http://ubuntuhandbook.org/index.php/2017/07/install-python-3-6-1-in-ubuntu-16-04-lts/
简短摘要:
sudo apt-get install python python-pip python3 python3-pip
sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2
然后
$ pip -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
$ pip3 -V
pip 8.1.1 from /usr/local/lib/python3.5/dist-packages (python 3.5)
然后选择python 3.6运行
sudo update-alternatives --config python3
并选择’2’。然后
$ pip3 -V
pip 8.1.1 from /usr/local/lib/python3.6/dist-packages (python 3.6)
要更新点,请选择所需的版本,然后
pip3 install --upgrade pip
$ pip3 -V
pip 9.0.1 from /usr/local/lib/python3.6/dist-packages (python 3.6)
在Ubuntu 16.04上测试。