3

Python学习笔记三pip介绍及国内镜像源加速

 2 years ago
source link: https://crazyrico.github.io/posts/2020/05/08/3ea54810.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

pip是一个以Python计算机程序语言写成的软件包管理系统,他可以安装和管理软件包,另外不少的软件包也可以在“Python软件包索引”(英语:Python Package Index,简称PyPI)中找到。
pip3就是Python3的pip。

20200507155359.png

命令行接口

pip 的其中一个主要特点就是其方便使用的命令行接口,这让用户可以透过以下的一句文字命令来轻易地安装 Python 软件包:

pip install <包名>

此外,用户也可以轻易地透过以下的命令来移除软件包:

pip uninstall <包名>

升级软件包:

pip install -U <包名>
或者
pip install <包名> --upgrade

pip 也拥有一个透过“需求”文件来管理软件包和其相应版本数目的完整列表之功能,这容许一个完整软件包组合可以在另一个环境(如另一部计算机)或虚拟化环境中进行有效率的重新创造。这个功能可以透过一个已正确进行格式化的文本文件和以下的命令来完成:

pip install -r requirements.txt

显示软件包所在的目录:

pip show -f <包名>
pip search <搜索关键字>

查询可升级的包:

pip list -o
pip wheel <包名>

在命令窗口输入pip3 install numpy,安装报错,出现一大堆类似下图红色的错误, 心中一万头草泥马

20200507174344.png

E:\Work\Code>python "Day 1_Data_Preprocessing.py"
Traceback (most recent call last):
File "Day 1_Data_Preprocessing.py", line 4, in <module>
import numpy as np
ModuleNotFoundError: No module named 'numpy'

E:\Work\Code>pip3 install numpy
Collecting numpy
Downloading https://files.pythonhosted.org/packages/99/29/080d63fb9579b426ea8081dc3f49f89e51912e0fb7d5be4a65cfa87e3898/numpy-1.18.3-cp37-cp37m-win_amd64.whl (12.8MB)
| | 10kB 434bytes/s eta 8:09:36ERROR: Exception:
Traceback (most recent call last):
File "d:\python\python37\lib\site-packages\pip\_vendor\urllib3\response.py", line 425, in _error_catcher
yield
File "d:\python\python37\lib\site-packages\pip\_vendor\urllib3\response.py", line 507, in read
data = self._fp.read(amt) if not fp_closed else b""
File "d:\python\python37\lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 62, in read
data = self.__fp.read(amt)
File "d:\python\python37\lib\http\client.py", line 457, in read
n = self.readinto(b)
File "d:\python\python37\lib\http\client.py", line 501, in readinto
n = self.fp.readinto(b)
File "d:\python\python37\lib\socket.py", line 589, in readinto
return self._sock.recv_into(b)
File "d:\python\python37\lib\ssl.py", line 1071, in recv_into
return self.read(nbytes, buffer)
File "d:\python\python37\lib\ssl.py", line 929, in read
return self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "d:\python\python37\lib\site-packages\pip\_internal\cli\base_command.py", line 153, in _main
status = self.run(options, args)
File "d:\python\python37\lib\site-packages\pip\_internal\commands\install.py", line 382, in run
resolver.resolve(requirement_set)
File "d:\python\python37\lib\site-packages\pip\_internal\legacy_resolve.py", line 201, in resolve
self._resolve_one(requirement_set, req)
File "d:\python\python37\lib\site-packages\pip\_internal\legacy_resolve.py", line 365, in _resolve_one
abstract_dist = self._get_abstract_dist_for(req_to_install)
File "d:\python\python37\lib\site-packages\pip\_internal\legacy_resolve.py", line 313, in _get_abstract_dist_for
req, self.session, self.finder, self.require_hashes
File "d:\python\python37\lib\site-packages\pip\_internal\operations\prepare.py", line 194, in prepare_linked_requirement
progress_bar=self.progress_bar
File "d:\python\python37\lib\site-packages\pip\_internal\download.py", line 465, in unpack_url
progress_bar=progress_bar
File "d:\python\python37\lib\site-packages\pip\_internal\download.py", line 316, in unpack_http_url
progress_bar)
File "d:\python\python37\lib\site-packages\pip\_internal\download.py", line 551, in _download_http_url
_download_url(resp, link, content_file, hashes, progress_bar)
File "d:\python\python37\lib\site-packages\pip\_internal\download.py", line 253, in _download_url
hashes.check_against_chunks(downloaded_chunks)
File "d:\python\python37\lib\site-packages\pip\_internal\utils\hashes.py", line 80, in check_against_chunks
for chunk in chunks:
File "d:\python\python37\lib\site-packages\pip\_internal\download.py", line 223, in written_chunks
for chunk in chunks:
File "d:\python\python37\lib\site-packages\pip\_internal\utils\ui.py", line 160, in iter
for x in it:
File "d:\python\python37\lib\site-packages\pip\_internal\download.py", line 212, in resp_read
decode_content=False):
File "d:\python\python37\lib\site-packages\pip\_vendor\urllib3\response.py", line 564, in stream
data = self.read(amt=amt, decode_content=decode_content)
File "d:\python\python37\lib\site-packages\pip\_vendor\urllib3\response.py", line 529, in read
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
File "d:\python\python37\lib\contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
File "d:\python\python37\lib\site-packages\pip\_vendor\urllib3\response.py", line 430, in _error_catcher
raise ReadTimeoutError(self._pool, None, "Read timed out.")
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
WARNING: You are using pip version 19.3.1; however, version 20.0.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

这是因为网络的问题,网络不畅下载软件包超时,可以使用国内的镜像源来加速,比如阿里源

pip3 install 加上 -i –trusted-host 阿里源镜像地址

E:\Work\Code>pip3 install numpy -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
Looking in indexes: http://mirrors.aliyun.com/pypi/simple
Collecting numpy
Downloading http://mirrors.aliyun.com/pypi/packages/99/29/080d63fb9579b426ea8081dc3f49f89e51912e0fb7d5be4a65cfa87e3898/numpy-1.18.3-cp37-cp37m-win_amd64.whl (12.8MB)
|████████████████████████████████| 12.8MB 6.8MB/s
Installing collected packages: numpy
Successfully installed numpy-1.18.3
WARNING: You are using pip version 19.3.1; however, version 20.0.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

perfect, 速度杠杠的

当然我还是喜欢一劳永逸的方法,每次install 都要带上一长串镜像地址参数实在
20200508092014.png

下面开始我的表演,进入系统 %AppData% 就是 C:\Users\用户名\AppData\Roaming
20200508092506.png
在这个目录下创建一个pip文件夹,在pip文件夹下新建pip.ini文件,打开pip.ini配置国内镜像源输入内容如下

[global]
timeout = 6000
index-url = http://mirrors.aliyun.com/pypi/simple
trusted-host = mirrors.aliyun.com

保存退出,欧了这样每次install 都会使用国内镜像加速

Linux下,修改 ~/.pip/pip.conf (没有就创建一个文件夹及文件。文件夹要加“.”,表示是隐藏文件夹)

[global]
index-url = http://mirrors.aliyun.com/pypi/simple
[install]
trusted-host = mirrors.aliyun.com

当然你还可以写几行python完成上面的操作,创建python文件(xx.py)

import os 

ini="""[global]
index-url = https://pypi.doubanio.com/simple/
[install]
trusted-host=pypi.doubanio.com
"""
pippath=os.environ["USERPROFILE"]+"\\pip\\"

if not os.path.exists(pippath):
os.mkdir(pippath)

with open(pippath+"pip.ini","w+") as f:
f.write(ini)

在cmd上运行这个xx.py 文件,效果与二相同

python .\xx.py

常用国内镜像

https://pypi.tuna.tsinghua.edu.cn/simple/	# 清华大学
https://mirrors.aliyun.com/pypi/simple/ # 阿里云
https://pypi.douban.com/simple/ # 豆瓣
https://pypi.mirrors.ustc.edu.cn/simple/ # 中国科学技术大学
https://pypi.hustunique.com/ # 华中科技大学

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK