python 使用pip install_万能python pip install大法

不知道大家有没有经常在pip python包的时候遇到下载超时、下载龟速、权限不够、pip版本太老或者是“During handling of the above exception, another exception occurred”的问题,头疼死了好吗。

题主总结了常见的pip下载问题,按照以下操作步骤更新pip,两行代码完美避开pip几乎所有的常见问题!!!

1. 用管理员身份运行并打开Anaconda Prompt

直接在任务栏右下角搜索框输入ana找到Anaconda Prompt,右键以管理员身份运行。

PS:Anaconda 是Python的一个发行版,与python不同的是里面内置了很多工具包,因此无需单独安装。如果读者下载的是Anaconda的话可以通过打开Anaconda Prompt更方便的切换到conda环境。

2. 二话不说先更新pip:

pip install --upgrade pip

3. 然后就是超牛逼代码:

使用清华镜像(-i )&防止超时(timeout)的命令结合,就可以完全避免下载龟速和下载超时的问题!!!比如我要下载rpy2:

python -m pip --default-timeout=100 install -i https://pypi.tuna.tsinghua.edu.cn/simple rpy2

然后清清爽爽干干净净快快速速的两行代码下载好这个包。。

想知道那行代码牛B的原因吗?大家经常头疼:

Traceback (most recent call last):

File "f:\program files (x86)\python36-32\lib\site-packages\pip\_vendor\urllib3\response.py", line 425, in _error_catcher

yield

File "f:\program files (x86)\python36-32\lib\site-packages\pip\_vendor\urllib3\response.py", line 507, in read

data = self._fp.read(amt) if not fp_closed else b""

File "f:\program files (x86)\python36-32\lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 62, in read

data = self.__fp.read(amt)

File "f:\program files (x86)\python36-32\lib\http\client.py", line 449, in read

n = self.readinto(b)

File "f:\program files (x86)\python36-32\lib\http\client.py", line 493, in readinto

n = self.fp.readinto(b)

File "f:\program files (x86)\python36-32\lib\socket.py", line 586, in readinto

return self._sock.recv_into(b)

File "f:\program files (x86)\python36-32\lib\ssl.py", line 1009, in recv_into

return self.read(nbytes, buffer)

File "f:\program files (x86)\python36-32\lib\ssl.py", line 871, in read

return self._sslobj.read(len, buffer)

File "f:\program files (x86)\python36-32\lib\ssl.py", line 631, in read

v = 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 "f:\program files (x86)\python36-32\lib\site-packages\pip\_internal\cli\base_command.py", line 186, in _main

status = self.run(options, args)

File "f:\program files (x86)\python36-32\lib\site-packages\pip\_internal\commands\install.py", line 331, in run

resolver.resolve(requirement_set)

File "f:\program files (x86)\python36-32\lib\site-packages\pip\_internal\legacy_resolve.py", line 177, in resolve

discovered_reqs.extend(self._resolve_one(requirement_set, req))

File "f:\program files (x86)\python36-32\lib\site-packages\pip\_internal\legacy_resolve.py", line 333, in _resolve_one

abstract_dist = self._get_abstract_dist_for(req_to_install)

File "f:\program files (x86)\python36-32\lib\site-packages\pip\_internal\legacy_resolve.py", line 282, in _get_abstract_dist_for

abstract_dist = self.preparer.prepare_linked_requirement(req)

File "f:\program files (x86)\python36-32\lib\site-packages\pip\_internal\operations\prepare.py", line 482, in prepare_linked_requirement

hashes=hashes,

File "f:\program files (x86)\python36-32\lib\site-packages\pip\_internal\operations\prepare.py", line 287, in unpack_url

hashes=hashes,

File "f:\program files (x86)\python36-32\lib\site-packages\pip\_internal\operations\prepare.py", line 159, in unpack_http_url

link, downloader, temp_dir.path, hashes

File "f:\program files (x86)\python36-32\lib\site-packages\pip\_internal\operations\prepare.py", line 303, in _download_http_url

for chunk in download.chunks:

File "f:\program files (x86)\python36-32\lib\site-packages\pip\_internal\utils\ui.py", line 160, in iter

for x in it:

File "f:\program files (x86)\python36-32\lib\site-packages\pip\_internal\network\utils.py", line 39, in response_chunks

decode_content=False,

File "f:\program files (x86)\python36-32\lib\site-packages\pip\_vendor\urllib3\response.py", line 564, in stream

data = self.read(amt=amt, decode_content=decode_content)

File "f:\program files (x86)\python36-32\lib\site-packages\pip\_vendor\urllib3\response.py", line 529, in read

raise IncompleteRead(self._fp_bytes_read, self.length_remaining)

File "f:\program files (x86)\python36-32\lib\contextlib.py", line 99, in __exit__

self.gen.throw(type, value, traceback)

File "f:\program files (x86)\python36-32\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.

方法一解决timeout的方法就需要在 pip的时候控制超时, 具体参数为“--default-timeout=100”,所以第二行代码的前一部分是:

python -m pip --default-timeout=100

方法二下载太慢的原因是因为如果直接下载包是连接国外的网站,所以只要使用清华镜像地址就完全莫问题!!(这里感谢清华爸爸们)所以可以在使用pip的时候加参数“-i 清华镜像地址”,而刚刚第二行代码的最后一部分是:

install -i https://pypi.tuna.tsinghua.edu.cn/simple rpy2

但是此网站慎点,因为没有做分页功能,点了会完全加载所有内容可能会使浏览器崩溃。

综合起来,这个万能代码就是(eg.下载rpy2包):

python -m pip --default-timeout=100 install -i https://pypi.tuna.tsinghua.edu.cn/simple rpy2

哈哈哈鼓掌吧兄弟们!!!学fei了吗!!

笔芯