ubuntu下python3.7安装pip3安装使用各种疑难杂症

Posted 奇妙之二进制

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ubuntu下python3.7安装pip3安装使用各种疑难杂症相关的知识,希望对你有一定的参考价值。

linux系统版本:ubuntu16.04
python版本:python3.7

ubuntu下安装python实在太麻烦了,牵扯各种版本,16.04默认安装了python2.7和python3.6,但python3.6太老了,很多的包都要求3.7及以上,我们今天演示如何安装python3.7以及它的pip。

如果我们直接在终端输入python,默认使用python2.7:

也可以使用python --version查看版本:

hongh@ubuntu:~$ python --version
Python 2.7.12

虽然安装了python2.7,但并没有安装pip:

hongh@ubuntu:~$ pip --version
The program 'pip' is currently not installed. You can install it by typing:
sudo apt install python-pip

提示告诉了我们安装方法:
sudo apt install python-pip
但我们不要。

pip3也同样没有安装,关于pip3和pip区别可以见:xxx。

hongh@ubuntu:~$ pip3
The program 'pip3' is currently not installed. You can install it by typing:
sudo apt install python3-pip

16.04还预置了python3.5:

hongh@ubuntu:~$ python3.5
Python 3.5.2 (default, Jan 26 2021, 13:30:48) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

我们先安装pip3:
sudo apt install python3-pip

查看pip3版本:

hongh@ubuntu:~$ pip3 --version
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

除了 版本信息,还可以看到安装位置。不过这个版本很老,我们后面进行升级。

pip3是什么东西,研究下:

hongh@ubuntu:~$ which pip3
/usr/bin/pip3
hongh@ubuntu:~$ cat /usr/bin/pip3 
#!/usr/bin/python3
# GENERATED BY DEBIAN

import sys

# Run the main entry point, similarly to how setuptools does it, but because
# we didn't install the actual entry point from setup.py, don't use the
# pkg_resources API.
from pip import main
if __name__ == '__main__':
    sys.exit(main())

发现只不过是个python3脚本,调用了pip包的main函数。
而pip包放在了/usr/lib/python3/dist-packages下,注意目录是python3,说明和版本无关,python3.5和python3.7都可以使用。

pip3可以对自己进行升级,我们来升级下pip3,:
注意,pip3虽然叫pip3,但它实际对应的是pip包,所以我们:
pip3 install pip而不是pip3 install pip3
有些会让加-U或者–upgrade选项,我试了不加也可以升级,但千万不要直接升级,因为我试过,升到最新版本,python3.5不支持,

hongh@ubuntu:~$ pip3 install pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/ca/bf/4133a0e05eac641ec270bbcef30512b5ad307d7838adb994acd652cc30e3/pip-21.2.3-py3-none-any.whl (1.6MB)
    100% |████████████████████████████████| 1.6MB 25kB/s 
Installing collected packages: pip
Successfully installed pip-21.2.3

升完级pip3就用不了了:

所以最好指定下版本,我试过了,18.0可以用:

接着安装python3.7。

ubuntu16.04安装python3.7有两种方法,见:
https://hongjh.blog.csdn.net/article/details/119458920。

python3.7和pip3装完之后,我们就可以使用python3.7 -m pip install xx安装python包了:

hongh@ubuntu:~$ python3.7 -m pip install pyuavcan
Collecting pyuavcan
  Using cached https://files.pythonhosted.org/packages/00/3e/98e9c2e12fb18cc23bb8f3f4b82a09b496dbbcf286f54f38b9cba30d67cc/pyuavcan-1.2.7-py3-none-any.whl
Collecting numpy<1.20,~=1.17 (from pyuavcan)
  Using cached https://files.pythonhosted.org/packages/b1/e1/8c4c5632adaffc18dba4e03e97458dc1cb00583811e6982fc620b9d88515/numpy-1.19.5-cp37-cp37m-manylinux1_x86_64.whl
Collecting nunavut~=1.2 (from pyuavcan)

  Using cached https://files.pythonhosted.org/packages/84/93/32f766ec89a26ae9e9cc10063a92e9dc37cb8faf7b5f8213f2d60298939b/nunavut-1.4.1-66-py3-none-any.whl
Collecting pyyaml (from nunavut~=1.2->pyuavcan)
  Using cached https://files.pythonhosted.org/packages/7a/a5/393c087efdc78091afa2af9f1378762f9821c9c1d7a22c5753fb5ac5f97a/PyYAML-5.4.1-cp37-cp37m-manylinux1_x86_64.whl
Collecting pydsdl~=1.12 (from nunavut~=1.2->pyuavcan)
  Using cached https://files.pythonhosted.org/packages/fe/76/34434ff2b780959e8ad3a0358d5432f2ac379a3b89f0d4f4e0c38b237b1d/pydsdl-1.12.1-py3-none-any.whl
Installing collected packages: numpy, pyyaml, pydsdl, nunavut, pyuavcan
Successfully installed numpy nunavut pydsdl pyuavcan pyyaml
You are using pip version 8.1.1, however version 21.2.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

接着我们再给python3.7安装pip包:

hongh@ubuntu:~$ python3.7 -m pip install pip
Collecting pip
  Using cached https://files.pythonhosted.org/packages/8a/d7/f505e91e2cdea53cfcf51f4ac478a8cd64fb0bc1042629cedde20d9a6a9b/pip-21.2.2-py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-21.2.2

看结果,直接安装到了最新版本。

卸载pip,从卸载输出可以看出pip安装在了/home/hongh/.local/lib/python3.7/site-packages目录,安装的包都会放在该目录下。

hongh@ubuntu:~$ python3.7 -m pip uninstall pip
Found existing installation: pip 21.2.2
Uninstalling pip-21.2.2:
  Would remove:
    /home/hongh/.local/bin/pip
    /home/hongh/.local/bin/pip3
    /home/hongh/.local/bin/pip3.7
    /home/hongh/.local/lib/python3.7/site-packages/pip-21.2.2.dist-info/*
    /home/hongh/.local/lib/python3.7/site-packages/pip/*
Proceed (Y/n)? y
  Successfully uninstalled pip-21.2.2

后续我们就可以用python3.7下的pip给python3.7安装其他包了:

python3.7 -m pip install pip

python3.7 -m pip install 包名

以上是关于ubuntu下python3.7安装pip3安装使用各种疑难杂症的主要内容,如果未能解决你的问题,请参考以下文章

[Linux] Ubuntu Server18 python3.7 虚拟环境

如何在 Ubuntu 18 上为 python 3.7 安装 pip?

pip3 安装 PyYAML 失败。(python3.7,macOS High Sierra)

python3的pip3安装

mac---pip3安装

pip3.7 install报错bad interpreter: No such file or directory