7

如何将python开源项目打包发布到pypi

 2 years ago
source link: https://foofish.net/python-packaging.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

这里我以我发布过的一个项目 flask-siwadoc 为例

编写 setup.py

# -*- encoding: UTF-8 -*-
try:
    from setuptools import setup, find_packages
except ImportError:
    from distutils.core import setup
import io

VERSION = '0.0.1'

with io.open("README.md", encoding='utf-8') as f:
    long_description = f.read()

install_requires = open("requirements.txt").readlines()

setup(
    name="flask-siwadoc",
    version=VERSION,
    author="liuzhijun",
    author_email="[email protected]",
    url="https://github.com/lzjun567/flask-siwadoc",
    description="flask openapi(swagger) doc generator",
    long_description=long_description,
    long_description_content_type="text/markdown",
    packages=["flask_siwadoc"],
    package_data={
        'flask_siwadoc': ['templates/*.html'],
    },
    include_package_data=True,
    license='MIT License',
    classifiers=[],
    python_requires=">=3.6",
    install_requires=install_requires,
    setup_requires=[
        'pytest-runner',
    ],
    tests_require=[
        "pytest",
    ],
)

这里几个注意的地方是指定打包的模块packages和要打包的数据package_data,其它看字段名基本都明白其中的意思。

安装打包以及上传包相关的库

twine 用于上传包至pypi

pip install -U setuptools wheel
pip install twine  
python setup.py sdist bdist_wheel 

pypi注册账号

在当前$HOME目录创建 .pypirc 文件

    [distutils]
    index-servers=
        pypi

    [pypi]
    username: brodan
    password: xxxxxxxxxxxxxxxx

检查是否符合pypi要求

twine check dist/*.tar.gz
twine upload --repository pypi dist/*

现在就可以通过 pip 命令下载你上传的包了

有问题可以扫描二维码和我交流

关注公众号「Python之禅」,回复「1024」免费获取Python资源

python之禅

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK