python 打包发布pypi package

introduction

有时候自己开发了一个python的package想要和他人分享,一个不 错的方案就是发布到pypi官网,这样大家都可以通过pip来安装。下面简单介绍下操作流程。

step 1:注册账号

登录pypi首页完成注册:
https://pypi.python.org/pypi

注册中需要邮箱,在完成页面注册后记得在邮箱中查看确认邮件完成邮箱认证


step 2:构建目录结构

这里默认大家都安装setuptools、pip,下面列出最简单的目录结构作为示例,根目录为funniest:

funniest/
    funniest/
        __init__.py
    setup.py

其中setup.py是打包的入口文件(entry point),其大致结构为:

from setuptools import setup

setup(name='funniest',
      version='0.1',
      description='The funniest joke in the world',
      url='http://github.com/storborg/funniest',
      author='Flying Circus',
      author_email='[email protected]',
      license='MIT',
      packages=['funniest'],
      zip_safe=False)

详细的参数配置参见官网文档:https://setuptools.readthedocs.io/en/latest/setuptools.html


压缩并发布

# 压缩
python setup.py sdist

命令在根目录下创建dist目录,并将压缩文件保存其中。

# 上传内容,需要twine(可以通过pip install twine安装)
twine upload dist/*

完成后在pypi官网登录后会查看到自己上传的包

Your packages:
funniest

其他人可以通过pip安装你上传的包

pip install funniest

附属链接

  1. twine github
  2. setuptools documents
  3. 发布你自己的轮子 - PyPI打包上传实践
  4. python-packaging
  5. setting-up-setup-py

猜你喜欢

转载自blog.csdn.net/u011077672/article/details/78890511