准备要发布的模块
自己写了个日志模块xlog,要把该模块发布到pypi,目录结构如下:
xlog
├── LICENSE
├── README.md
├── setup.py
├── tests
└── xlog
├── __init__.py
└── info.py
LICENSE:许可证,根据情况选一个
setup.py:打包用到的配置
xlog:核心代码
tests:测试代码
# info.py
import datetime
def info(msg):
print(datetime.datetime.now(), msg)
setup.py
给setuptools提供一些模块相关的信息,示例:
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="xlog",
version="1.0",
author="xxx",
author_email="xxx@xxx.com",
description="",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/xxx",
packages=setuptools.find_packages(),
classifiers=[
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Topic :: Software Development",
],
install_requires=[
],
python_requires='>=3.6',
)
默认情况下只打包py文件,如果包含其它文件比如.so格式,增加以下配置:
package_data={
"xlog": [
"*.py",
"*.so",
]
},
打包
安装打包依赖:
pip install -U setuptools wheel
打包:
python setup.py sdist bdist_wheel
生成build、dist、xlog.egg-info三个目录,把dist目录上传至pypi即可。
上传
需要事先注册pypi账号
安装发布工具:
pip install -U twine
发布:
twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
新注册的账号第一次发布的时候会报错,意思是邮箱未激活,登陆邮箱即可看到提醒激活的邮件。
测试
pip install xlog
from xlog import info
print(info.info("xxx"))
其他
在打包之前,最好先在本地测试,确保正确再发布
本地测试
在项目目录执行
python setup.py install
然后在其他项目中pip install测试。