當前位置: 首頁>>技術教程>>正文


如何在Ubuntu 16.10上為Python 3.6安裝pip?

, , ,

問題描述

首先,我想指出這個問題似乎是重複的,但事實並非如此。我在這裏看到的所有問題都是關於Python 3的pip,而我正在談論的是Python 3.6。那時使用的步驟不適用於Python 3.6。

  1. 我從official docker store獲得了清晰的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

最佳方法

假設您有一個運行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中,默認的python3python3.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

此後,可以預期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)

第四種方法

該網站包含一個更幹淨的解決方案,它使點碼完整無缺,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上測試。

參考資料

本文由Ubuntu問答整理, 博文地址: https://ubuntuqa.com/zh-tw/article/8447.html,未經允許,請勿轉載。