学习了部分Python的基础知识以后,大伟哥打算边做边学,用Python写一个和工作相关的爬虫脚本,毕竟学东西就是要拿来用的,学来不用,就不会产生价值,也就没有任何意义了。这次大伟哥需要安装额外的模块比如requests模块来使用已有的功能,而要安装和管理Python的模块,使用pip无疑是一个方便快捷的工具:
pip 是一个现代的,通用的 Python 包管理工具。提供了对 Python 包的查找、下载、安装、卸载的功能。
在Ubuntu 16.04上面安装Python3版本的pip工具很方便,直接sudo apt install python3-pip就可以了:
daweibro@Workstation:~$ sudo apt install python3-pip Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libpython3-dev libpython3.5 libpython3.5-dev libpython3.5-minimal libpython3.5-stdlib python-pip-whl python3-dev python3-setuptools python3-wheel python3.5 python3.5-dev python3.5-minimal Suggested packages: python-setuptools-doc python3.5-venv python3.5-doc The following NEW packages will be installed: libpython3-dev libpython3.5-dev python-pip-whl python3-dev python3-pip python3-setuptools python3-wheel python3.5-dev The following packages will be upgraded: libpython3.5 libpython3.5-minimal libpython3.5-stdlib python3.5 python3.5-minimal 5 upgraded, 8 newly installed, 0 to remove and 42 not upgraded. Need to get 43.5 MB/44.9 MB of archives. After this operation, 56.4 MB of additional disk space will be used. Do you want to continue? [Y/n]
按Y安装完成后,查看pip的版本,发现是对应Ubuntu系统自带的Python 3.5:
daweibro@Workstation:~$ pip3 -V 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:
daweibro@Workstation:~$ pip3 list | grep requests requests (2.20.1)
而大伟哥正在学习使用的是Python3.6,系统自带的requests版本仍然是2.9.1版本:
daweibro@Workstation:~$ python3.6 -m pip list | grep requests 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升级对应的模块。这种方法虽然麻烦一点,但是不会和系统产生任何冲突,适用于大伟哥这样的编程小白。
daweibro@Workstation:~$ python3.6 -m pip install --upgrade requests Collecting requests Using cached https://files.pythonhosted.org/packages/ff/17/5cbb026005115301a8fb2f9b0e3e8d32313142fe8b617070e7baad20554f/requests-2.20.1-py2.py3-none-any.whl Collecting idna<2.8,>=2.5 (from requests) Using cached https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl Collecting urllib3<1.25,>=1.21.1 (from requests) Using cached https://files.pythonhosted.org/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl Collecting chardet<3.1.0,>=3.0.2 (from requests) Using cached https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl Collecting certifi>=2017.4.17 (from requests) Using cached https://files.pythonhosted.org/packages/56/9d/1d02dd80bc4cd955f98980f28c5ee2200e1209292d5f9e9cc8d030d18655/certifi-2018.10.15-py2.py3-none-any.whl Installing collected packages: idna, urllib3, chardet, certifi, requests Successfully installed certifi-2018.10.15 chardet-3.0.4 idna-2.7 requests-2.20.1 urllib3-1.24.1 You are using pip version 8.1.1, however version 18.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command. daweibro@Workstation:~$ python3.6 -m pip list | grep requests requests (2.20.1) You are using pip version 8.1.1, however version 18.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
现在可以在Ubuntu 16.04下面使用Python 3.6和最新的requests模块了。
- 2460 阅读
添加新评论