打包
参照官方文档:
- setup规范 https://packaging.python.org/tutorials/distributing-packages/#setup-py
- twine使方法 https://pypi.org/project/twine/
工程目录
packaging_tutorial/
├── LICENSE
├── pyproject.toml
├── README.md
├── setup.py
├── src/
│ └── example_package/
│ ├── __init__.py
│ └── example.py
└── tests/
setup.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import setuptools
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
setuptools.setup(
name="ccr",
version="0.0.1",
description="python 基础包",
long_description=long_description,
long_description_content_type="text/markdown",
# url="",
# project_urls={
# "Bug Tracker": "",
# },
classifiers=[
"Development Status :: 4 - Beta",
"Programming Language :: Python :: 3.7",
"Operating System :: OS Independent",
],
package_dir={"": "src"},
packages=setuptools.find_packages(where="src"),
python_requires=">=3.7",
install_requires=[
"kafka_python==2.0.2",
"pymongo==3.11.2",
"requests==2.25.1",
"PyHDFS==0.3.1",
"asana_kazoo==2.0.8dev",
"kafka==1.3.5",
],
)
打包命令
python setup.py sdist bdist_wheel
打包命令在工程根目录下运行。
上传
python包的上传使用twine包
安装twine包
pip install twine
上传配置
在用户根目录下添加.pypirc文件。
- windows 用户目录:C:\Users\XXX.pypirc
- linux用户目录:/home/XXX/.pypirc
添加如下配置:
[distutils]
index-servers =
nexus
[nexus]
repository=http://192.168.1.201:9527/repository/pypi-release/
username=ccreport
password=ccreport
说明:
- [distutils]指定私服配置标识,也可加入pypi官方仓库配置,指定多个仓库地址在下一行即可,例如:
[distutils]
index-servers =
nexus
pypi
- [nexus] 配置私服仓库地址。
- repository指定仓库url
- username/password指定登录nexus私服的用户名和密码
上传命令
twine upload -r nexus dist/*
说明:
-
命令在工程根目录下执行,命令的含义为:将工程根目录中
dist
目录下的所有文件上传到-r
指定的仓库 -
-r
参数可以选择仓库地址,本例中选用的仓库是nexus,如果在pypirc文件中配置了官方仓库的地址,可以使用-r指定pypi官方仓库地址。
使用上传到私服的包
使用时使用命令安装即可。
pip install CCRUtils -i http://192.168.1.201:9527/repository/pypi-group/simple --trusted-host 192.168.1.201
说明:
-i
或--index-url
指定index-url
参数,即索引包名的主库搜索地址.--extra-index-url
指定其他包的附加库索引地址。
也可以更改环境本地的pip.conf
或者pip.ini
配置文件。
例如:
[global]
index = http://192.168.1.201:9527/repository/pypi-group/pypi
index-url = http://192.168.1.201:9527/repository/pypi-group/simple
trusted-host = 192.168.1.201