前言
做个笔记,少简陋,不要见怪啦~
开始
典型错误案例
现在请开始看错误做法,预计使用3
分钟
1
(base) localhost:~ *马赛克*$ pip install opencv
ERROR: Could not find a version that satisfies the requirement opencv
ERROR: No matching distribution found for opencv
2
(base) localhost:~ *马赛克*$ pip install cv2
ERROR: Could not find a version that satisfies the requirement cv2
ERROR: No matching distribution found for cv2
3
(base) localhost:~ *马赛克*$ pip install python-opencv
ERROR: Could not find a version that satisfies the requirement python-opencv
ERROR: No matching distribution found for python-opencv
正确安装方法
其实第三种做法已经很接近真理了,正确做法应该是
pip install opencv-python
俗话说“万物皆会超时”,如果你没有,那么可以跳到这里
你看,那红红的超时多美(qiàn)丽(zòu)
ERROR: Exception:
Traceback (most recent call last):
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_vendor/resolvelib/resolvers.py", line 171, in _merge_into_criterion
crit = self.state.criteria[name]
KeyError: 'opencv-python'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_vendor/urllib3/response.py", line 438, in _error_catcher
yield
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_vendor/urllib3/response.py", line 519, in read
data = self._fp.read(amt) if not fp_closed else b""
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_vendor/cachecontrol/filewrapper.py", line 62, in read
data = self.__fp.read(amt)
File "/Users/*马赛克*/anaconda3/lib/python3.7/http/client.py", line 447, in read
n = self.readinto(b)
File "/Users/*马赛克*/anaconda3/lib/python3.7/http/client.py", line 491, in readinto
n = self.fp.readinto(b)
File "/Users/*马赛克*/anaconda3/lib/python3.7/socket.py", line 589, in readinto
return self._sock.recv_into(b)
File "/Users/*马赛克*/anaconda3/lib/python3.7/ssl.py", line 1052, in recv_into
return self.read(nbytes, buffer)
File "/Users/*马赛克*/anaconda3/lib/python3.7/ssl.py", line 911, 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 "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/cli/base_command.py", line 189, in _main
status = self.run(options, args)
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/cli/req_command.py", line 178, in wrapper
return func(self, options, args)
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/commands/install.py", line 317, in run
reqs, check_supported_wheels=not options.target_dir
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/resolution/resolvelib/resolver.py", line 122, in resolve
requirements, max_rounds=try_to_avoid_resolution_too_deep,
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_vendor/resolvelib/resolvers.py", line 453, in resolve
state = resolution.resolve(requirements, max_rounds=max_rounds)
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_vendor/resolvelib/resolvers.py", line 318, in resolve
name, crit = self._merge_into_criterion(r, parent=None)
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_vendor/resolvelib/resolvers.py", line 173, in _merge_into_criterion
crit = Criterion.from_requirement(self._p, requirement, parent)
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_vendor/resolvelib/resolvers.py", line 82, in from_requirement
if not cands:
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_vendor/resolvelib/structs.py", line 124, in __bool__
return bool(self._sequence)
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 143, in __bool__
return any(self)
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 38, in _iter_built
candidate = func()
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/resolution/resolvelib/factory.py", line 169, in _make_candidate_from_link
name=name, version=version,
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 306, in __init__
version=version,
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 144, in __init__
self.dist = self._prepare()
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 226, in _prepare
dist = self._prepare_distribution()
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 312, in _prepare_distribution
self._ireq, parallel_builds=True,
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/operations/prepare.py", line 457, in prepare_linked_requirement
return self._prepare_linked_requirement(req, parallel_builds)
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/operations/prepare.py", line 482, in _prepare_linked_requirement
self.download_dir, hashes,
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/operations/prepare.py", line 234, in unpack_url
hashes=hashes,
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/operations/prepare.py", line 108, in get_http_url
from_path, content_type = download(link, temp_dir.path)
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/network/download.py", line 163, in __call__
for chunk in chunks:
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/cli/progress_bars.py", line 159, in iter
for x in it:
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_internal/network/utils.py", line 88, in response_chunks
decode_content=False,
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_vendor/urllib3/response.py", line 576, in stream
data = self.read(amt=amt, decode_content=decode_content)
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_vendor/urllib3/response.py", line 541, in read
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
File "/Users/*马赛克*/anaconda3/lib/python3.7/contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
File "/Users/*马赛克*/anaconda3/lib/python3.7/site-packages/pip/_vendor/urllib3/response.py", line 443, 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.
解决方案一
现在请所有人和我一起高呼:“镜像大法好!”
管他是清华镜像或是豆瓣镜像,能用就是好镜像
pip install opencv-python -i <你的镜像url>
这里给大家列几个镜像源
镜像名称 | 镜像url |
---|---|
清华 | https://pypi.tuna.tsinghua.edu.cn/simple |
阿里云 | http://mirrors.aliyun.com/pypi/simple/ |
中国科技大学 | https://pypi.mirrors.ustc.edu.cn/simple/ |
华中理工大学 | http://pypi.hustunique.com/ |
山东理工大学 | http://pypi.sdutlinux.org/ |
豆瓣 | http://pypi.douban.com/simple/ |
具体方案见《python pip使用镜像源》
解决方案2
方案2呢,你得有足够的毅力和耐心,不按Ctrl+C
,因为这个方案是更改超时时间
pip install python-opencv -default-timeout=10000
成功?
安装成功了吗?
当你看到如下消息时,恭喜你,安装失败
ERROR: Exception:
............
当看到如下消息时,还要恭喜你,安装失败
WARNING: Retrying (Retry(total=..., connect=None, read=None, redirect=None, status=None)) after connection broken by ............
当看到如下消息时,太可惜了,安装成功
(base) localhost:~ *马赛克*$ pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting opencv-python
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/81/44/8f37035f0558e597108e5aa7952f7b422a6eb1275b24f7d31027171700d0/opencv_python-4.5.1.48-cp37-cp37m-macosx_10_13_x86_64.whl (40.3 MB)
|████████████████████████████████| 40.3 MB 4.9 MB/s
Requirement already satisfied: numpy>=1.14.5 in ./anaconda3/lib/python3.7/site-packages (from opencv-python) (1.15.1)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.5.1.48
检验
如果进行如下检验操作
(base) localhost:~ *马赛克*$ python
Python 3.7.2 (default, Dec 29 2018, 00:00:04)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>
能够不报错,就是安装成功
作者
hit-road
拜拜,下课!
hit-road不定期跟新,不见不散!