天天看點

打包自定義python子產品釋出到pypi

準備要釋出的子產品

自己寫了個日志子產品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="[email protected]", 
    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測試。