Ubuntu 16.04下安装及使用Python3-pip

大伟哥 提交于 2018-11-16,周五, 21:27

学习了部分Python的基础知识以后,大伟哥打算边做边学,用Python写一个和工作相关的爬虫脚本,毕竟学东西就是要拿来用的,学来不用,就不会产生价值,也就没有任何意义了。这次大伟哥需要安装额外的模块比如requests模块来使用已有的功能,而要安装和管理Python的模块,使用pip无疑是一个方便快捷的工具:

pip 是一个现代的,通用的 Python 包管理工具。提供了对 Python 包的查找、下载、安装、卸载的功能。

在Ubuntu 16.04上面安装Python3版本的pip工具很方便,直接sudo apt install python3-pip就可以了:

  1. daweibro@Workstation:~$ sudo apt install python3-pip
  2. Reading package lists... Done
  3. Building dependency tree
  4. Reading state information... Done
  5. The following additional packages will be installed:
  6. libpython3-dev libpython3.5 libpython3.5-dev libpython3.5-minimal
  7. libpython3.5-stdlib python-pip-whl python3-dev python3-setuptools
  8. python3-wheel python3.5 python3.5-dev python3.5-minimal
  9. Suggested packages:
  10. python-setuptools-doc python3.5-venv python3.5-doc
  11. The following NEW packages will be installed:
  12. libpython3-dev libpython3.5-dev python-pip-whl python3-dev python3-pip
  13. python3-setuptools python3-wheel python3.5-dev
  14. The following packages will be upgraded:
  15. libpython3.5 libpython3.5-minimal libpython3.5-stdlib python3.5
  16. python3.5-minimal
  17. 5 upgraded, 8 newly installed, 0 to remove and 42 not upgraded.
  18. Need to get 43.5 MB/44.9 MB of archives.
  19. After this operation, 56.4 MB of additional disk space will be used.
  20. Do you want to continue? [Y/n]

按Y安装完成后,查看pip的版本,发现是对应Ubuntu系统自带的Python 3.5:

  1. daweibro@Workstation:~$ pip3 -V
  2. pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

这时候如果使用pip install命令安装Python模块,应该是关联到默认的 Python 3.5 版本。大伟哥使用 pip3 install requests 命令安装好以后,发现安装的 requests 版本是2.20.1:

  1. daweibro@Workstation:~$ pip3 list | grep requests
  2. requests (2.20.1)

而大伟哥正在学习使用的是Python3.6,系统自带的requests版本仍然是2.9.1版本:

  1. daweibro@Workstation:~$ python3.6 -m pip list | grep requests
  2. requests (2.9.1)

第一个解决办法,是改变Ubuntu的默认Python3版本,让Python3版本直接默认运行Python 3.6,这种方式只需要修改一次,不过有很少的可能会导致Ubuntu系统出错。另外一种方法,是保留系统默认的Python版本不变,而是使用python3.6 -m pip install命令给Python 3.6安装需要的模块,使用python3.6 -m pip install --upgrade命令给Python 3.6升级对应的模块。这种方法虽然麻烦一点,但是不会和系统产生任何冲突,适用于大伟哥这样的编程小白。

  1. daweibro@Workstation:~$ python3.6 -m pip install --upgrade requests
  2. Collecting requests
  3. Using cached https://files.pythonhosted.org/packages/ff/17/5cbb026005115301a8fb2f9b0e3e8d32313142fe8b617070e7baad20554f/requests-2.20.1-py2.py3-none-any.whl
  4. Collecting idna<2.8,>=2.5 (from requests)
  5. Using cached https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl
  6. Collecting urllib3<1.25,>=1.21.1 (from requests)
  7. Using cached https://files.pythonhosted.org/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl
  8. Collecting chardet<3.1.0,>=3.0.2 (from requests)
  9. Using cached https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl
  10. Collecting certifi>=2017.4.17 (from requests)
  11. Using cached https://files.pythonhosted.org/packages/56/9d/1d02dd80bc4cd955f98980f28c5ee2200e1209292d5f9e9cc8d030d18655/certifi-2018.10.15-py2.py3-none-any.whl
  12. Installing collected packages: idna, urllib3, chardet, certifi, requests
  13. Successfully installed certifi-2018.10.15 chardet-3.0.4 idna-2.7 requests-2.20.1 urllib3-1.24.1
  14. You are using pip version 8.1.1, however version 18.1 is available.
  15. You should consider upgrading via the 'pip install --upgrade pip' command.
  16. daweibro@Workstation:~$ python3.6 -m pip list | grep requests
  17. requests (2.20.1)
  18. You are using pip version 8.1.1, however version 18.1 is available.
  19. You should consider upgrading via the 'pip install --upgrade pip' command.

现在可以在Ubuntu 16.04下面使用Python 3.6和最新的requests模块了。

文章分类

标签

添加新评论

此字段内容将保密,不会被其他人看见。

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。