记录一下python3安装pip3的过程

今天要运行一个python脚本,但是需要requests包(好多python程序都会用到这个包),所有就需要pip3来安装,但是发现我的python3并没有安装pip3,特此记录下其安装过程;

已有环境:

  • debian
  • python3

apt upgrade

下载pip安装脚本需要用到curl:

1
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py   # 下载安装脚本

但是电脑上目前没有,需要先安装,apt安装时报以下错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@wgq ~]# apt install curl
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libcurl4
The following NEW packages will be installed:
curl libcurl4
0 upgraded, 2 newly installed, 0 to remove and 23 not upgraded.
Need to get 596 kB of archives.
After this operation, 1,123 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Err:1 http://mirrors.ustc.edu.cn/debian stable/main amd64 libcurl4 amd64 7.64.0-4
404 Not Found [IP: 202.38.95.110 80]
Err:2 http://mirrors.ustc.edu.cn/debian stable/main amd64 curl amd64 7.64.0-4
404 Not Found [IP: 202.38.95.110 80]
E: Failed to fetch http://mirrors.ustc.edu.cn/debian/pool/main/c/curl/libcurl4_7.64.0-4_amd64.deb 404 Not Found [IP: 202.38.95.110 80]
E: Failed to fetch http://mirrors.ustc.edu.cn/debian/pool/main/c/curl/curl_7.64.0-4_amd64.deb 404 Not Found [IP: 202.38.95.110 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

执行:

1
apt upgrade

然后重新安装即可;

很多时候,apt安装失败,可以先执行apt升级

安装 pip

在上一步中,我们已经下载好了安装pip3需要的脚本,这时直接运行即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 运行安装脚本,我装的python3,并且没有创建python软链接指向python3,在环境变量中只有python3并没有python命名,所以这里直接用python3

[root@wgq ~]# python3 get-pip.py
Traceback (most recent call last):
File "get-pip.py", line 23484, in <module>
main()
File "get-pip.py", line 198, in main
bootstrap(tmpdir=tmpdir)
File "get-pip.py", line 82, in bootstrap
from pip._internal.cli.main import main as pip_entry_point
File "/tmp/tmpsdeg2046/pip.zip/pip/_internal/cli/main.py", line 10, in <module>
File "/tmp/tmpsdeg2046/pip.zip/pip/_internal/cli/autocompletion.py", line 9, in <module>
File "/tmp/tmpsdeg2046/pip.zip/pip/_internal/cli/main_parser.py", line 7, in <module>
File "/tmp/tmpsdeg2046/pip.zip/pip/_internal/cli/cmdoptions.py", line 19, in <module>
ModuleNotFoundError: No module named 'distutils.util'

结果报错了,仔细阅读错误提示,最后一行提示缺少一个模块,去网上找了下解决办法,如下,安装python3-distutils即可:

1
apt-get install python3-distutils

然后重新安装pip3:

1
2
3
4
5
6
7
8
9
10
11
[root@wgq ~]# python3 get-pip.py
Collecting pip
Downloading pip-20.1.1-py2.py3-none-any.whl (1.5 MB)
|████████████████████████████████| 1.5 MB 10 kB/s
Collecting setuptools
Downloading setuptools-49.1.0-py3-none-any.whl (789 kB)
|████████████████████████████████| 789 kB 10 kB/s
Collecting wheel
Downloading wheel-0.34.2-py2.py3-none-any.whl (26 kB)
Installing collected packages: pip, setuptools, wheel
Successfully installed pip-20.1.1 setuptools-49.1.0 wheel-0.34.2

安装成功!

安装requests包

这个时候,已经安装好了pip3,直接安装requests包即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
[root@wgq /mnt/d/Download]# pip3 install requests
Collecting requests
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")': /packages/45/1e/0c169c6a5381e241ba7404532c16a21d86ab872c9bed8bdcd4c423954103/requests-2.24.0-py2.py3-none-any.whl
Downloading requests-2.24.0-py2.py3-none-any.whl (61 kB)
|████████████████████████████████| 61 kB 22 kB/s
Collecting chardet<4,>=3.0.2
Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
|██████████████████████▏ | 92 kB 6.0 kB/s eta 0:00:07ERROR: Exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/pip/_vendor/urllib3/response.py", line 425, in _error_catcher
yield
File "/usr/local/lib/python3.7/dist-packages/pip/_vendor/urllib3/response.py", line 507, in read
data = self._fp.read(amt) if not fp_closed else b""
File "/usr/local/lib/python3.7/dist-packages/pip/_vendor/cachecontrol/filewrapper.py", line 62, in read
data = self.__fp.read(amt)
File "/usr/lib/python3.7/http/client.py", line 457, in read
n = self.readinto(b)
File "/usr/lib/python3.7/http/client.py", line 501, in readinto
n = self.fp.readinto(b)
File "/usr/lib/python3.7/socket.py", line 589, in readinto
return self._sock.recv_into(b)
File "/usr/lib/python3.7/ssl.py", line 1052, in recv_into
return self.read(nbytes, buffer)
File "/usr/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 "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/base_command.py", line 188, in _main
status = self.run(options, args)
File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/req_command.py", line 185, in wrapper
return func(self, options, args)
File "/usr/local/lib/python3.7/dist-packages/pip/_internal/commands/install.py", line 333, in run
reqs, check_supported_wheels=not options.target_dir
File "/usr/local/lib/python3.7/dist-packages/pip/_internal/resolution/legacy/resolver.py", line 179, in resolve
discovered_reqs.extend(self._resolve_one(requirement_set, req))
File "/usr/local/lib/python3.7/dist-packages/pip/_internal/resolution/legacy/resolver.py", line 362, in _resolve_one
abstract_dist = self._get_abstract_dist_for(req_to_install)
File "/usr/local/lib/python3.7/dist-packages/pip/_internal/resolution/legacy/resolver.py", line 314, in _get_abstract_dist_for
abstract_dist = self.preparer.prepare_linked_requirement(req)
File "/usr/local/lib/python3.7/dist-packages/pip/_internal/operations/prepare.py", line 469, in prepare_linked_requirement
hashes=hashes,
File "/usr/local/lib/python3.7/dist-packages/pip/_internal/operations/prepare.py", line 259, in unpack_url
hashes=hashes,
File "/usr/local/lib/python3.7/dist-packages/pip/_internal/operations/prepare.py", line 130, in get_http_url
link, downloader, temp_dir.path, hashes
File "/usr/local/lib/python3.7/dist-packages/pip/_internal/operations/prepare.py", line 281, in _download_http_url
for chunk in download.chunks:
File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/progress_bars.py", line 166, in iter
for x in it:
File "/usr/local/lib/python3.7/dist-packages/pip/_internal/network/utils.py", line 39, in response_chunks
decode_content=False,
File "/usr/local/lib/python3.7/dist-packages/pip/_vendor/urllib3/response.py", line 564, in stream
data = self.read(amt=amt, decode_content=decode_content)
File "/usr/local/lib/python3.7/dist-packages/pip/_vendor/urllib3/response.py", line 529, in read
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
File "/usr/lib/python3.7/contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
File "/usr/local/lib/python3.7/dist-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.

执行报错了,仔细阅读错误,发现是超时错误,并不是其他错误,重新试一遍即可:

一遇到错误不要立马复制粘贴错误到网上去瞎找,自己先仔细阅读一下错误是什么,尝试去解决,还是不行的话,有针对性地去网上找!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@wgq /mnt/d/Download]# pip3 install requests
Collecting requests
Using cached requests-2.24.0-py2.py3-none-any.whl (61 kB)
Collecting chardet<4,>=3.0.2
Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
|████████████████████████████████| 133 kB 15 kB/s
Collecting idna<3,>=2.5
Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
|████████████████████████████████| 58 kB 40 kB/s
Collecting certifi>=2017.4.17
Downloading certifi-2020.6.20-py2.py3-none-any.whl (156 kB)
|████████████████████████████████| 156 kB 56 kB/s
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
Downloading urllib3-1.25.9-py2.py3-none-any.whl (126 kB)
|████████████████████████████████| 126 kB 56 kB/s
Installing collected packages: chardet, idna, certifi, urllib3, requests
Successfully installed certifi-2020.6.20 chardet-3.0.4 idna-2.10 requests-2.24.0 urllib3-1.25.9

安装成功!

pip升级

1
2
[root@wgq ~]# pip install -U pip
Requirement already up-to-date: pip in /usr/local/lib/python3.7/dist-packages (20.1.1)

如果上面的命令提示权限问题,可以用下面的:

1
2
3
4
[root@wgq ~]# easy_install --upgrade pip
WARNING: The easy_install command is deprecated and will be removed in a future version.
Searching for pip
Reading https://pypi.org/simple/pip/

等执行完毕即可!

参考:

评论