通过命令行往python私仓传包
文章发布较早,内容可能过时,阅读注意甄别。
# 1,前言
提供了 Python 私仓之后,除了通过私仓拉包方便之外,小伙伴们难免会有往私仓传包的需求,因为稍微有点绕弯,因此这里记录一下。
其实用一句话来表述,就是通过私服拉包的时候,走group的代理,从本地上传包的时候,要走local的仓库。
接下来就记录一下配置方法并用测试包来验证下。
创建私服的过程这里不详述了,已知现有 Python 私仓,物料信息如下:
- group:http://nexus.test.com/repository/pyg/
- local:http://nexus.test.com/repository/pyg/
# 2,上传
# 1,先安装依赖包
$ pip3 install wheel --user
$ pip3 install twine --user
1
2
2
# 2,添加配置
有了如上物料之后,我们首先来验证下上传功能,在本地用户家目录添加如下配置:
$ cat ~/.pypirc
[distutils]
index-servers =
pypi
nexus
[pypi]
repository:https://pypi.python.org/pypi
username:eryajf
password:test
[nexus]
repository=http://nexus.test.com/repository/pyg/
username=dev
password=GVLaX-E|yq$vE[/k
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 3,编写代码
然后在本地打一个测试包
$ mkdir helloworld
$ touch helloworld/__init__.py
$ cat helloworld/demo.py
class Demo:
@staticmethod
def hello():
print('helloworld')
1
2
3
4
5
6
7
2
3
4
5
6
7
编写 setup.py:
$ cat setup.py
from setuptools import setup
setup(
name='helloworld',
version='1.0',
author="techlog",
license="MIT",
packages=[
'helloworld'
],
install_requires=[
],
classifiers=[
"Topic :: Utilities",
"Topic :: Internet",
"Topic :: Software Development :: Libraries :: Python Modules"
],
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
具体的 setup.py 的编写规范可以参考:https://packaging.python.org/tutorials/distributing-packages/#setup-py
目录结构:
$ tree
.
├── helloworld
│ ├── __init__.py
│ └── demo.py
└── setup.py
1 directory, 3 files
1
2
3
4
5
6
7
2
3
4
5
6
7
# 4,打包
$ python3 setup.py sdist bdist_wheel
running sdist
running egg_info
creating helloworld.egg-info
writing helloworld.egg-info/PKG-INFO
writing dependency_links to helloworld.egg-info/dependency_links.txt
writing top-level names to helloworld.egg-info/top_level.txt
writing manifest file 'helloworld.egg-info/SOURCES.txt'
reading manifest file 'helloworld.egg-info/SOURCES.txt'
writing manifest file 'helloworld.egg-info/SOURCES.txt'
warning: sdist: standard file not found: should have one of README, README.rst, README.txt, README.md
running check
warning: check: missing required meta-data: url
warning: check: missing meta-data: if 'author' supplied, 'author_email' must be supplied too
creating helloworld-1.0
creating helloworld-1.0/helloworld
creating helloworld-1.0/helloworld.egg-info
copying files to helloworld-1.0...
copying setup.py -> helloworld-1.0
copying helloworld/__init__.py -> helloworld-1.0/helloworld
copying helloworld/demo.py -> helloworld-1.0/helloworld
copying helloworld.egg-info/PKG-INFO -> helloworld-1.0/helloworld.egg-info
copying helloworld.egg-info/SOURCES.txt -> helloworld-1.0/helloworld.egg-info
copying helloworld.egg-info/dependency_links.txt -> helloworld-1.0/helloworld.egg-info
copying helloworld.egg-info/top_level.txt -> helloworld-1.0/helloworld.egg-info
Writing helloworld-1.0/setup.cfg
creating dist
Creating tar archive
removing 'helloworld-1.0' (and everything under it)
running bdist_wheel
running build
running build_py
creating build
creating build/lib
creating build/lib/helloworld
copying helloworld/__init__.py -> build/lib/helloworld
copying helloworld/demo.py -> build/lib/helloworld
installing to build/bdist.macosx-10.14-x86_64/wheel
running install
running install_lib
creating build/bdist.macosx-10.14-x86_64
creating build/bdist.macosx-10.14-x86_64/wheel
creating build/bdist.macosx-10.14-x86_64/wheel/helloworld
copying build/lib/helloworld/__init__.py -> build/bdist.macosx-10.14-x86_64/wheel/helloworld
copying build/lib/helloworld/demo.py -> build/bdist.macosx-10.14-x86_64/wheel/helloworld
running install_egg_info
Copying helloworld.egg-info to build/bdist.macosx-10.14-x86_64/wheel/helloworld-1.0-py3.7.egg-info
running install_scripts
creating build/bdist.macosx-10.14-x86_64/wheel/helloworld-1.0.dist-info/WHEEL
creating 'dist/helloworld-1.0-py3-none-any.whl' and adding 'build/bdist.macosx-10.14-x86_64/wheel' to it
adding 'helloworld/__init__.py'
adding 'helloworld/demo.py'
adding 'helloworld-1.0.dist-info/METADATA'
adding 'helloworld-1.0.dist-info/WHEEL'
adding 'helloworld-1.0.dist-info/top_level.txt'
adding 'helloworld-1.0.dist-info/RECORD'
removing build/bdist.macosx-10.14-x86_64/wheel
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
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
打包之后,会生成一个 build 目录和 dist 目录:
$ tree
.
├── build
│ ├── bdist.macosx-10.14-x86_64
│ └── lib
│ └── helloworld
│ ├── __init__.py
│ └── demo.py
├── dist
│ ├── helloworld-1.0-py3-none-any.whl
│ └── helloworld-1.0.tar.gz
├── helloworld
│ ├── __init__.py
│ └── demo.py
├── helloworld.egg-info
│ ├── PKG-INFO
│ ├── SOURCES.txt
│ ├── dependency_links.txt
│ └── top_level.txt
└── setup.py
7 directories, 11 files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 5,上传
$ python3 -m twine upload --repository nexus dist/*
Uploading distributions to http://nexus.test.com/repository/pygl/
Uploading helloworld-1.0-py3-none-any.whl
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4.38k/4.38k [00:00<00:00, 46.9kB/s]
Uploading helloworld-1.0.tar.gz
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3.77k/3.77k [00:00<00:00, 112kB/s]
1
2
3
4
5
6
2
3
4
5
6
看到如上内容,就说明上传成功了。
可以浏览器上看下:
# 3,下载
拉包就比较简单了,首先配置如下信息:
mkdir ~/.pip
cat > ~/.pip/pip.conf << EOF
[global]
timeout = 60
trusted-host = nexus.test.com
index-url = http://nexus.test.com/repository/pypi/simple
EOF
1
2
3
4
5
6
7
2
3
4
5
6
7
然后直接安装对应包即可。
上次更新: 2024/11/19, 23:11:42