cnpip 1.0.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cnpip-1.0.5/LICENSE +21 -0
- cnpip-1.0.5/PKG-INFO +178 -0
- cnpip-1.0.5/README.md +151 -0
- cnpip-1.0.5/cnpip/__init__.py +2 -0
- cnpip-1.0.5/cnpip/cnpip.py +160 -0
- cnpip-1.0.5/cnpip/mirrors.py +8 -0
- cnpip-1.0.5/cnpip.egg-info/PKG-INFO +178 -0
- cnpip-1.0.5/cnpip.egg-info/SOURCES.txt +13 -0
- cnpip-1.0.5/cnpip.egg-info/dependency_links.txt +1 -0
- cnpip-1.0.5/cnpip.egg-info/entry_points.txt +2 -0
- cnpip-1.0.5/cnpip.egg-info/requires.txt +1 -0
- cnpip-1.0.5/cnpip.egg-info/top_level.txt +1 -0
- cnpip-1.0.5/pyproject.toml +36 -0
- cnpip-1.0.5/setup.cfg +4 -0
- cnpip-1.0.5/setup.py +32 -0
cnpip-1.0.5/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Eric Cao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
cnpip-1.0.5/PKG-INFO
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: cnpip
|
|
3
|
+
Version: 1.0.5
|
|
4
|
+
Summary: 帮助中国用户快速切换 pip 镜像源,提升下载速度的命令行工具.
|
|
5
|
+
Home-page: https://github.com/caoergou/cnpip
|
|
6
|
+
Author: caoergou
|
|
7
|
+
Author-email: caoergou <mrjingcheng@foxmail.com>
|
|
8
|
+
Project-URL: Homepage, https://github.com/caoergou/cnpip
|
|
9
|
+
Project-URL: Issues, https://github.com/caoergou/cnpip/issues
|
|
10
|
+
Keywords: pip,mirror,China,speed,cli
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Classifier: Topic :: System :: Installation/Setup
|
|
22
|
+
Classifier: Topic :: Utilities
|
|
23
|
+
Requires-Python: >=3.7
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: requests
|
|
27
|
+
|
|
28
|
+
# cnpip
|
|
29
|
+
|
|
30
|
+

|
|
31
|
+

|
|
32
|
+

|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
`cnpip` 是一个帮助你快速切换 `pip` 镜像源,提升 Python 包的下载速度的命令行工具。
|
|
36
|
+
它可以测试各镜像源的连接速度,并**自动选择最快的镜像源**。
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
## 快速使用
|
|
40
|
+
|
|
41
|
+
运行以下命令,快速切换为最快的镜像源:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install cnpip
|
|
45
|
+
cnpip set
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## 功能
|
|
49
|
+
|
|
50
|
+
- **列出并测试镜像源速度**,按连接速度排序
|
|
51
|
+
- **快速切换 pip 镜像源**,支持*手动选择*或*自动选择*最快镜像
|
|
52
|
+
|
|
53
|
+
## 支持的镜像源
|
|
54
|
+
|
|
55
|
+
- [清华大学 TUNA](https://pypi.tuna.tsinghua.edu.cn/simple)
|
|
56
|
+
- [阿里云](https://mirrors.aliyun.com/pypi/simple)
|
|
57
|
+
- [中国科学技术大学](https://pypi.mirrors.ustc.edu.cn/simple)
|
|
58
|
+
- [豆瓣](https://pypi.douban.com/simple)(目前可能无法连接)
|
|
59
|
+
- [默认源](https://pypi.org/simple)
|
|
60
|
+
|
|
61
|
+
## 使用方法
|
|
62
|
+
|
|
63
|
+
### 1. 列出所有可用的镜像源
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
cnpip list
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
示例输出:
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
镜像名称 耗时(ms) 地址
|
|
73
|
+
--------------------------------------------------
|
|
74
|
+
ustc 135.71 https://pypi.mirrors.ustc.edu.cn/simple
|
|
75
|
+
aliyun 300.77 https://mirrors.aliyun.com/pypi/simple
|
|
76
|
+
tuna 499.51 https://pypi.tuna.tsinghua.edu.cn/simple
|
|
77
|
+
default 1252.75 https://pypi.org/simple
|
|
78
|
+
douban error https://pypi.douban.com/simple
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 2. 自动选择最快的镜像源
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
cnpip set
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
示例输出:
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
未指定镜像源,自动选择最快的镜像源: ustc
|
|
91
|
+
成功设置 pip 镜像源为 'https://pypi.mirrors.ustc.edu.cn/simple'
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 3. 选择指定的镜像源
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
cnpip set <镜像名称>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
示例:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
cnpip set tuna
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
输出:
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
成功设置 pip 镜像源为 'https://pypi.tuna.tsinghua.edu.cn/simple'
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### 4. 取消自定义镜像源设置
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
cnpip unset
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
输出:
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
成功取消 pip 镜像源设置,已恢复为默认源。
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## 配置文件
|
|
125
|
+
|
|
126
|
+
`cnpip` 会修改或创建 `pip` 的配置文件来设置镜像源:
|
|
127
|
+
|
|
128
|
+
- **Linux/macOS**: `~/.pip/pip.conf`
|
|
129
|
+
- **Windows**: `%APPDATA%\pip\pip.ini`
|
|
130
|
+
|
|
131
|
+
在设置镜像源时,`cnpip` 只会修改或添加 `index-url` 配置,不会覆盖其他配置项。
|
|
132
|
+
|
|
133
|
+
## 常见问题
|
|
134
|
+
|
|
135
|
+
### 1. 为什么我无法连接到某些镜像源?
|
|
136
|
+
|
|
137
|
+
某些镜像源(如豆瓣)可能由于网络问题或镜像源本身的原因无法连接。在这种情况下,`cnpip` 会显示“无法连接”,并将其排在速度测试结果的最后。
|
|
138
|
+
|
|
139
|
+
### 2. 如何恢复为默认的 `pip` 镜像源?
|
|
140
|
+
|
|
141
|
+
使用 `unset` 命令恢复为默认的 `pip` 镜像源:
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
cnpip unset
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### 3. `cnpip` 会覆盖我的 `pip.conf` 文件吗?
|
|
148
|
+
|
|
149
|
+
不会。`cnpip` 只会修改或添加 `index-url` 配置项,其他配置项会被保留。
|
|
150
|
+
|
|
151
|
+
## 许可证
|
|
152
|
+
|
|
153
|
+
本项目使用 [MIT 许可证](LICENSE)。
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
# cnpip (English)
|
|
158
|
+
|
|
159
|
+
`cnpip` is a command-line tool designed specifically for users in **mainland China** to help quickly switch `pip`
|
|
160
|
+
mirrors and improve Python package download speeds.
|
|
161
|
+
It tests the connection speed of various mirrors and **automatically selects the fastest one**.
|
|
162
|
+
|
|
163
|
+
> **Attention: This Python package is only available in Chinese mainland.**
|
|
164
|
+
|
|
165
|
+
## Quick Start
|
|
166
|
+
|
|
167
|
+
Run the following commands to quickly switch to the fastest mirror:
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
pip install cnpip
|
|
171
|
+
cnpip set
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Features
|
|
175
|
+
|
|
176
|
+
- **List and test mirror speeds**, sorted by connection time
|
|
177
|
+
- **Quickly switch pip mirrors**, supporting *manual selection* or *automatic selection* of the fastest mirror
|
|
178
|
+
- Designed specifically for users in mainland China
|
cnpip-1.0.5/README.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# cnpip
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
`cnpip` 是一个帮助你快速切换 `pip` 镜像源,提升 Python 包的下载速度的命令行工具。
|
|
9
|
+
它可以测试各镜像源的连接速度,并**自动选择最快的镜像源**。
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## 快速使用
|
|
13
|
+
|
|
14
|
+
运行以下命令,快速切换为最快的镜像源:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install cnpip
|
|
18
|
+
cnpip set
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 功能
|
|
22
|
+
|
|
23
|
+
- **列出并测试镜像源速度**,按连接速度排序
|
|
24
|
+
- **快速切换 pip 镜像源**,支持*手动选择*或*自动选择*最快镜像
|
|
25
|
+
|
|
26
|
+
## 支持的镜像源
|
|
27
|
+
|
|
28
|
+
- [清华大学 TUNA](https://pypi.tuna.tsinghua.edu.cn/simple)
|
|
29
|
+
- [阿里云](https://mirrors.aliyun.com/pypi/simple)
|
|
30
|
+
- [中国科学技术大学](https://pypi.mirrors.ustc.edu.cn/simple)
|
|
31
|
+
- [豆瓣](https://pypi.douban.com/simple)(目前可能无法连接)
|
|
32
|
+
- [默认源](https://pypi.org/simple)
|
|
33
|
+
|
|
34
|
+
## 使用方法
|
|
35
|
+
|
|
36
|
+
### 1. 列出所有可用的镜像源
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
cnpip list
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
示例输出:
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
镜像名称 耗时(ms) 地址
|
|
46
|
+
--------------------------------------------------
|
|
47
|
+
ustc 135.71 https://pypi.mirrors.ustc.edu.cn/simple
|
|
48
|
+
aliyun 300.77 https://mirrors.aliyun.com/pypi/simple
|
|
49
|
+
tuna 499.51 https://pypi.tuna.tsinghua.edu.cn/simple
|
|
50
|
+
default 1252.75 https://pypi.org/simple
|
|
51
|
+
douban error https://pypi.douban.com/simple
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 2. 自动选择最快的镜像源
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
cnpip set
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
示例输出:
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
未指定镜像源,自动选择最快的镜像源: ustc
|
|
64
|
+
成功设置 pip 镜像源为 'https://pypi.mirrors.ustc.edu.cn/simple'
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 3. 选择指定的镜像源
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
cnpip set <镜像名称>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
示例:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
cnpip set tuna
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
输出:
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
成功设置 pip 镜像源为 'https://pypi.tuna.tsinghua.edu.cn/simple'
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 4. 取消自定义镜像源设置
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
cnpip unset
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
输出:
|
|
92
|
+
|
|
93
|
+
```
|
|
94
|
+
成功取消 pip 镜像源设置,已恢复为默认源。
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## 配置文件
|
|
98
|
+
|
|
99
|
+
`cnpip` 会修改或创建 `pip` 的配置文件来设置镜像源:
|
|
100
|
+
|
|
101
|
+
- **Linux/macOS**: `~/.pip/pip.conf`
|
|
102
|
+
- **Windows**: `%APPDATA%\pip\pip.ini`
|
|
103
|
+
|
|
104
|
+
在设置镜像源时,`cnpip` 只会修改或添加 `index-url` 配置,不会覆盖其他配置项。
|
|
105
|
+
|
|
106
|
+
## 常见问题
|
|
107
|
+
|
|
108
|
+
### 1. 为什么我无法连接到某些镜像源?
|
|
109
|
+
|
|
110
|
+
某些镜像源(如豆瓣)可能由于网络问题或镜像源本身的原因无法连接。在这种情况下,`cnpip` 会显示“无法连接”,并将其排在速度测试结果的最后。
|
|
111
|
+
|
|
112
|
+
### 2. 如何恢复为默认的 `pip` 镜像源?
|
|
113
|
+
|
|
114
|
+
使用 `unset` 命令恢复为默认的 `pip` 镜像源:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
cnpip unset
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### 3. `cnpip` 会覆盖我的 `pip.conf` 文件吗?
|
|
121
|
+
|
|
122
|
+
不会。`cnpip` 只会修改或添加 `index-url` 配置项,其他配置项会被保留。
|
|
123
|
+
|
|
124
|
+
## 许可证
|
|
125
|
+
|
|
126
|
+
本项目使用 [MIT 许可证](LICENSE)。
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
# cnpip (English)
|
|
131
|
+
|
|
132
|
+
`cnpip` is a command-line tool designed specifically for users in **mainland China** to help quickly switch `pip`
|
|
133
|
+
mirrors and improve Python package download speeds.
|
|
134
|
+
It tests the connection speed of various mirrors and **automatically selects the fastest one**.
|
|
135
|
+
|
|
136
|
+
> **Attention: This Python package is only available in Chinese mainland.**
|
|
137
|
+
|
|
138
|
+
## Quick Start
|
|
139
|
+
|
|
140
|
+
Run the following commands to quickly switch to the fastest mirror:
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
pip install cnpip
|
|
144
|
+
cnpip set
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Features
|
|
148
|
+
|
|
149
|
+
- **List and test mirror speeds**, sorted by connection time
|
|
150
|
+
- **Quickly switch pip mirrors**, supporting *manual selection* or *automatic selection* of the fastest mirror
|
|
151
|
+
- Designed specifically for users in mainland China
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
import argparse
|
|
4
|
+
import requests
|
|
5
|
+
import time
|
|
6
|
+
import configparser
|
|
7
|
+
from urllib.parse import urlparse
|
|
8
|
+
from mirrors import MIRRORS
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def get_pip_config_path():
|
|
12
|
+
if os.name == 'nt': # Windows
|
|
13
|
+
return os.path.join(os.getenv('APPDATA'), 'pip', 'pip.ini')
|
|
14
|
+
else: # Linux and macOS
|
|
15
|
+
return os.path.expanduser('~/.pip/pip.conf')
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_mirror_speed(url):
|
|
19
|
+
try:
|
|
20
|
+
start_time = time.time()
|
|
21
|
+
response = requests.head(url, timeout=5) # 发送 HEAD 请求
|
|
22
|
+
response.raise_for_status() # 检查请求是否成功
|
|
23
|
+
end_time = time.time()
|
|
24
|
+
return round((end_time - start_time) * 1000, 2) # 返回毫秒级的响应时间
|
|
25
|
+
except requests.RequestException:
|
|
26
|
+
return None # 如果请求失败,返回 None
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def list_mirrors():
|
|
30
|
+
results = []
|
|
31
|
+
for name, url in MIRRORS.items():
|
|
32
|
+
speed = test_mirror_speed(url)
|
|
33
|
+
results.append((name, speed, url))
|
|
34
|
+
|
|
35
|
+
results.sort(key=lambda x: (x[1] is None, x[1]))
|
|
36
|
+
|
|
37
|
+
name_width = max(len(name) for name in MIRRORS.keys()) + 2
|
|
38
|
+
time_width = 10 # 固定宽度,确保耗时列对齐
|
|
39
|
+
url_width = max(len(url) for url in MIRRORS.values()) + 2
|
|
40
|
+
|
|
41
|
+
header = f"{'镜像名称':<{name_width}} {'耗时(ms)':<{time_width}} {'地址':<{url_width}}"
|
|
42
|
+
print(header)
|
|
43
|
+
print("-" * (name_width + time_width + url_width))
|
|
44
|
+
|
|
45
|
+
# 打印排序后的结果
|
|
46
|
+
for name, speed, url in results:
|
|
47
|
+
if speed is not None:
|
|
48
|
+
print(f"{name:<{name_width}} {speed:<{time_width}.2f} {url:<{url_width}}")
|
|
49
|
+
else:
|
|
50
|
+
print(f"{name:<{name_width}} {'error':<{time_width}} {url:<{url_width}}")
|
|
51
|
+
|
|
52
|
+
return results # 返回测速结果
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def extract_host_from_url(url):
|
|
56
|
+
parsed_url = urlparse(url)
|
|
57
|
+
return parsed_url.hostname
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def update_pip_config(mirror_url):
|
|
61
|
+
config_path = get_pip_config_path()
|
|
62
|
+
config_dir = os.path.dirname(config_path)
|
|
63
|
+
|
|
64
|
+
if not os.path.exists(config_dir):
|
|
65
|
+
os.makedirs(config_dir)
|
|
66
|
+
|
|
67
|
+
config = configparser.ConfigParser()
|
|
68
|
+
|
|
69
|
+
if os.path.exists(config_path):
|
|
70
|
+
config.read(config_path)
|
|
71
|
+
|
|
72
|
+
# 确保有 [global] 部分
|
|
73
|
+
if 'global' not in config:
|
|
74
|
+
config['global'] = {}
|
|
75
|
+
|
|
76
|
+
# 设置 index-url
|
|
77
|
+
config['global']['index-url'] = mirror_url
|
|
78
|
+
|
|
79
|
+
# 提取主机名并设置 trusted-host
|
|
80
|
+
host = extract_host_from_url(mirror_url)
|
|
81
|
+
if 'trusted-host' in config['global']:
|
|
82
|
+
existing_hosts = config['global']['trusted-host'].split()
|
|
83
|
+
if host not in existing_hosts:
|
|
84
|
+
existing_hosts.append(host)
|
|
85
|
+
config['global']['trusted-host'] = ' '.join(existing_hosts)
|
|
86
|
+
else:
|
|
87
|
+
config['global']['trusted-host'] = host
|
|
88
|
+
|
|
89
|
+
with open(config_path, 'w') as configfile:
|
|
90
|
+
config.write(configfile)
|
|
91
|
+
|
|
92
|
+
print(f"成功设置 pip 镜像源为 '{mirror_url}',并添加 trusted-host '{host}'")
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def set_mirror(mirror_name=None):
|
|
96
|
+
if mirror_name is None:
|
|
97
|
+
print("未指定镜像源,正在测速并选择最快的镜像源...\n")
|
|
98
|
+
results = list_mirrors()
|
|
99
|
+
|
|
100
|
+
fastest_mirror = next((name for name, speed, url in results if speed is not None), None)
|
|
101
|
+
|
|
102
|
+
if fastest_mirror is None:
|
|
103
|
+
print("错误: 无法连接到任何镜像源。")
|
|
104
|
+
sys.exit(1)
|
|
105
|
+
|
|
106
|
+
mirror_name = fastest_mirror
|
|
107
|
+
print(f"\n自动选择最快的镜像源: {mirror_name}")
|
|
108
|
+
|
|
109
|
+
elif mirror_name not in MIRRORS:
|
|
110
|
+
print(f"错误: 未找到镜像源 '{mirror_name}'。")
|
|
111
|
+
sys.exit(1)
|
|
112
|
+
|
|
113
|
+
mirror_url = MIRRORS[mirror_name]
|
|
114
|
+
|
|
115
|
+
update_pip_config(mirror_url)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def unset_mirror():
|
|
119
|
+
config_path = get_pip_config_path()
|
|
120
|
+
|
|
121
|
+
if os.path.exists(config_path):
|
|
122
|
+
config = configparser.ConfigParser()
|
|
123
|
+
config.read(config_path)
|
|
124
|
+
|
|
125
|
+
if 'global' in config and 'index-url' in config['global']:
|
|
126
|
+
del config['global']['index-url']
|
|
127
|
+
|
|
128
|
+
if 'trusted-host' in config['global']:
|
|
129
|
+
del config['global']['trusted-host']
|
|
130
|
+
|
|
131
|
+
if not config['global']:
|
|
132
|
+
del config['global']
|
|
133
|
+
|
|
134
|
+
with open(config_path, 'w') as configfile:
|
|
135
|
+
config.write(configfile)
|
|
136
|
+
|
|
137
|
+
print("成功取消 pip 镜像源设置,已恢复为默认源。")
|
|
138
|
+
else:
|
|
139
|
+
print("未设置自定义 pip 镜像源。")
|
|
140
|
+
else:
|
|
141
|
+
print("未设置自定义 pip 镜像源。")
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def main():
|
|
145
|
+
parser = argparse.ArgumentParser(description="轻松管理 pip 镜像源。")
|
|
146
|
+
parser.add_argument("command", choices=["list", "set", "unset"], help="要执行的命令")
|
|
147
|
+
parser.add_argument("mirror", nargs="?", help="要设置的镜像源名称 (仅用于 'set' 命令)")
|
|
148
|
+
|
|
149
|
+
args = parser.parse_args()
|
|
150
|
+
|
|
151
|
+
if args.command == "list":
|
|
152
|
+
list_mirrors()
|
|
153
|
+
elif args.command == "set":
|
|
154
|
+
set_mirror(args.mirror)
|
|
155
|
+
elif args.command == "unset":
|
|
156
|
+
unset_mirror()
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
if __name__ == "__main__":
|
|
160
|
+
main()
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
MIRRORS = {
|
|
2
|
+
"tuna": "https://pypi.tuna.tsinghua.edu.cn/simple",
|
|
3
|
+
"aliyun": "https://mirrors.aliyun.com/pypi/simple",
|
|
4
|
+
"ustc": "https://pypi.mirrors.ustc.edu.cn/simple",
|
|
5
|
+
"tencent": "https://mirrors.cloud.tencent.com/pypi/simple",
|
|
6
|
+
"huawei": "https://repo.huaweicloud.com/repository/pypi/simple",
|
|
7
|
+
"default": "https://pypi.org/simple"
|
|
8
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: cnpip
|
|
3
|
+
Version: 1.0.5
|
|
4
|
+
Summary: 帮助中国用户快速切换 pip 镜像源,提升下载速度的命令行工具.
|
|
5
|
+
Home-page: https://github.com/caoergou/cnpip
|
|
6
|
+
Author: caoergou
|
|
7
|
+
Author-email: caoergou <mrjingcheng@foxmail.com>
|
|
8
|
+
Project-URL: Homepage, https://github.com/caoergou/cnpip
|
|
9
|
+
Project-URL: Issues, https://github.com/caoergou/cnpip/issues
|
|
10
|
+
Keywords: pip,mirror,China,speed,cli
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Classifier: Topic :: System :: Installation/Setup
|
|
22
|
+
Classifier: Topic :: Utilities
|
|
23
|
+
Requires-Python: >=3.7
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: requests
|
|
27
|
+
|
|
28
|
+
# cnpip
|
|
29
|
+
|
|
30
|
+

|
|
31
|
+

|
|
32
|
+

|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
`cnpip` 是一个帮助你快速切换 `pip` 镜像源,提升 Python 包的下载速度的命令行工具。
|
|
36
|
+
它可以测试各镜像源的连接速度,并**自动选择最快的镜像源**。
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
## 快速使用
|
|
40
|
+
|
|
41
|
+
运行以下命令,快速切换为最快的镜像源:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install cnpip
|
|
45
|
+
cnpip set
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## 功能
|
|
49
|
+
|
|
50
|
+
- **列出并测试镜像源速度**,按连接速度排序
|
|
51
|
+
- **快速切换 pip 镜像源**,支持*手动选择*或*自动选择*最快镜像
|
|
52
|
+
|
|
53
|
+
## 支持的镜像源
|
|
54
|
+
|
|
55
|
+
- [清华大学 TUNA](https://pypi.tuna.tsinghua.edu.cn/simple)
|
|
56
|
+
- [阿里云](https://mirrors.aliyun.com/pypi/simple)
|
|
57
|
+
- [中国科学技术大学](https://pypi.mirrors.ustc.edu.cn/simple)
|
|
58
|
+
- [豆瓣](https://pypi.douban.com/simple)(目前可能无法连接)
|
|
59
|
+
- [默认源](https://pypi.org/simple)
|
|
60
|
+
|
|
61
|
+
## 使用方法
|
|
62
|
+
|
|
63
|
+
### 1. 列出所有可用的镜像源
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
cnpip list
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
示例输出:
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
镜像名称 耗时(ms) 地址
|
|
73
|
+
--------------------------------------------------
|
|
74
|
+
ustc 135.71 https://pypi.mirrors.ustc.edu.cn/simple
|
|
75
|
+
aliyun 300.77 https://mirrors.aliyun.com/pypi/simple
|
|
76
|
+
tuna 499.51 https://pypi.tuna.tsinghua.edu.cn/simple
|
|
77
|
+
default 1252.75 https://pypi.org/simple
|
|
78
|
+
douban error https://pypi.douban.com/simple
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 2. 自动选择最快的镜像源
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
cnpip set
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
示例输出:
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
未指定镜像源,自动选择最快的镜像源: ustc
|
|
91
|
+
成功设置 pip 镜像源为 'https://pypi.mirrors.ustc.edu.cn/simple'
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 3. 选择指定的镜像源
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
cnpip set <镜像名称>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
示例:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
cnpip set tuna
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
输出:
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
成功设置 pip 镜像源为 'https://pypi.tuna.tsinghua.edu.cn/simple'
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### 4. 取消自定义镜像源设置
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
cnpip unset
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
输出:
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
成功取消 pip 镜像源设置,已恢复为默认源。
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## 配置文件
|
|
125
|
+
|
|
126
|
+
`cnpip` 会修改或创建 `pip` 的配置文件来设置镜像源:
|
|
127
|
+
|
|
128
|
+
- **Linux/macOS**: `~/.pip/pip.conf`
|
|
129
|
+
- **Windows**: `%APPDATA%\pip\pip.ini`
|
|
130
|
+
|
|
131
|
+
在设置镜像源时,`cnpip` 只会修改或添加 `index-url` 配置,不会覆盖其他配置项。
|
|
132
|
+
|
|
133
|
+
## 常见问题
|
|
134
|
+
|
|
135
|
+
### 1. 为什么我无法连接到某些镜像源?
|
|
136
|
+
|
|
137
|
+
某些镜像源(如豆瓣)可能由于网络问题或镜像源本身的原因无法连接。在这种情况下,`cnpip` 会显示“无法连接”,并将其排在速度测试结果的最后。
|
|
138
|
+
|
|
139
|
+
### 2. 如何恢复为默认的 `pip` 镜像源?
|
|
140
|
+
|
|
141
|
+
使用 `unset` 命令恢复为默认的 `pip` 镜像源:
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
cnpip unset
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### 3. `cnpip` 会覆盖我的 `pip.conf` 文件吗?
|
|
148
|
+
|
|
149
|
+
不会。`cnpip` 只会修改或添加 `index-url` 配置项,其他配置项会被保留。
|
|
150
|
+
|
|
151
|
+
## 许可证
|
|
152
|
+
|
|
153
|
+
本项目使用 [MIT 许可证](LICENSE)。
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
# cnpip (English)
|
|
158
|
+
|
|
159
|
+
`cnpip` is a command-line tool designed specifically for users in **mainland China** to help quickly switch `pip`
|
|
160
|
+
mirrors and improve Python package download speeds.
|
|
161
|
+
It tests the connection speed of various mirrors and **automatically selects the fastest one**.
|
|
162
|
+
|
|
163
|
+
> **Attention: This Python package is only available in Chinese mainland.**
|
|
164
|
+
|
|
165
|
+
## Quick Start
|
|
166
|
+
|
|
167
|
+
Run the following commands to quickly switch to the fastest mirror:
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
pip install cnpip
|
|
171
|
+
cnpip set
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Features
|
|
175
|
+
|
|
176
|
+
- **List and test mirror speeds**, sorted by connection time
|
|
177
|
+
- **Quickly switch pip mirrors**, supporting *manual selection* or *automatic selection* of the fastest mirror
|
|
178
|
+
- Designed specifically for users in mainland China
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
setup.py
|
|
5
|
+
cnpip/__init__.py
|
|
6
|
+
cnpip/cnpip.py
|
|
7
|
+
cnpip/mirrors.py
|
|
8
|
+
cnpip.egg-info/PKG-INFO
|
|
9
|
+
cnpip.egg-info/SOURCES.txt
|
|
10
|
+
cnpip.egg-info/dependency_links.txt
|
|
11
|
+
cnpip.egg-info/entry_points.txt
|
|
12
|
+
cnpip.egg-info/requires.txt
|
|
13
|
+
cnpip.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
requests
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
cnpip
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "cnpip"
|
|
7
|
+
version = "1.0.5"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "caoergou", email = "mrjingcheng@foxmail.com" },
|
|
10
|
+
]
|
|
11
|
+
description = "帮助中国用户快速切换 pip 镜像源,提升下载速度的命令行工具.\nA tool that helps Chinese users quickly switch pip mirrors to improve download speeds."
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.7"
|
|
14
|
+
keywords = ["pip", "mirror", "China", "speed", "cli"]
|
|
15
|
+
dependencies = ["requests"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.7",
|
|
22
|
+
"Programming Language :: Python :: 3.8",
|
|
23
|
+
"Programming Language :: Python :: 3.9",
|
|
24
|
+
"Programming Language :: Python :: 3.10",
|
|
25
|
+
"Programming Language :: Python :: 3.11",
|
|
26
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
27
|
+
"Topic :: System :: Installation/Setup",
|
|
28
|
+
"Topic :: Utilities"
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.urls]
|
|
32
|
+
Homepage = "https://github.com/caoergou/cnpip"
|
|
33
|
+
Issues = "https://github.com/caoergou/cnpip/issues"
|
|
34
|
+
|
|
35
|
+
[project.scripts]
|
|
36
|
+
cnpip = "cnpip.cnpip:main"
|
cnpip-1.0.5/setup.cfg
ADDED
cnpip-1.0.5/setup.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name="cnpip",
|
|
5
|
+
version="1.0.5",
|
|
6
|
+
description="帮助中国用户快速切换 pip 镜像源,提升下载速度的命令行工具/A tool that helps Chinese users quickly switch pip mirrors to improve download speeds.",
|
|
7
|
+
author="caoergou",
|
|
8
|
+
author_email="itsericsmail@gmail.com",
|
|
9
|
+
url="https://github.com/caoergou/cnpip",
|
|
10
|
+
packages=find_packages(),
|
|
11
|
+
entry_points={
|
|
12
|
+
'console_scripts': [
|
|
13
|
+
'cnpip=cnpip.cnpip:main',
|
|
14
|
+
],
|
|
15
|
+
},
|
|
16
|
+
classifiers=[
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.7",
|
|
21
|
+
"Programming Language :: Python :: 3.8",
|
|
22
|
+
"Programming Language :: Python :: 3.9",
|
|
23
|
+
"Programming Language :: Python :: 3.10",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
26
|
+
"Topic :: System :: Installation/Setup",
|
|
27
|
+
"Topic :: Utilities",
|
|
28
|
+
],
|
|
29
|
+
install_requires=[
|
|
30
|
+
"requests"
|
|
31
|
+
]
|
|
32
|
+
)
|