注册账号(重要)

https://pypi.org

可以配置到$HOME/.pypirc文件中,就不用多次输入了

[pypi]
username = <username>
password = <password>

windows可以参考我之前的文章
Python编程:为世界贡献你的轮子-pipy打包

创建项目

创建一个名为 example_pkg 的项目,目录结构如下

example_pkg/example_pkg__init__.py

编辑文件 example_pkg/__init__.py

name = "example_pkg"

创建包文件

/example_pkg/example_pkg__init__.pysetup.pyLICENSEREADME.md

创建 setup.py

按照自己的信息,逐项填写即可

import setuptools
import os
import requests# 将markdown格式转换为rst格式
def md_to_rst(from_file, to_file):r = requests.post(url='http://c.docverter.com/convert',data={'to':'rst','from':'markdown'},files={'input_files[]':open(from_file,'rb')})if r.ok:with open(to_file, "wb") as f:f.write(r.content)md_to_rst("README.md", "README.rst")if os.path.exists('README.rst'):long_description = open('README.rst', encoding="utf-8").read()
else:long_description = 'Add a fallback short description here'if os.path.exists("requirements.txt"):install_requires = io.open("requirements.txt").read().split("\n")
else:install_requires = []setuptools.setup(name="chinesename",version="0.0.8",author="Peng Shiyu",license = 'MIT License',  author_email="pengshiyuyx@gmail.com",description="get a chinesename by random",long_description=long_description,long_description_content_type="text/x-rst",url="https://github.com/mouday/chinesename",packages=setuptools.find_packages(),classifiers=("Programming Language :: Python :: 3","License :: OSI Approved :: MIT License","Operating System :: OS Independent",),install_requires = install_requires,       # 常用# include_package_data=True,  # 自动打包文件夹内所有数据# 如果需要包含多个文件可以单独配置 MANIFEST.inpackage_data = {# If any package contains *.txt or *.rst files, include them:'chinesename': ['source/*.txt', "source/*.json"],},# 如果需要支持脚本方法运行,可以配置入口点entry_points={'console_scripts': ['chinesename = chinesename.run:main']}
)

关于setup.py文件可以参考官方给的例子:
https://github.com/pypa/sampleproject/blob/master/setup.py

创建 README.md

建议写的详细些,展示你项目的主要介绍

 # Example PackageThis is a simple example package. You can use
[Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/)
to write your content.

官网上说支持markdown格式,可是显示不正确,可以将.md文件转为.rst文件, 也有推荐说使用 Pandoc装换,我没成功,所以使用了setup.py中的方法

Have the same README both in Markdown and reStructuredText

生成目录树,添加文件目录说明:

tree /F > tree.txt

创建 LICENSE

可以忽略

可参考:https://choosealicense.com/

Copyright (c) 2018 The Python Packaging AuthorityPermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

生成发布压缩包

确保已经安装setuptoolswheel

python3 -m pip install --user --upgrade setuptools wheel

setup.py文件同目录命令行下运行

python3 setup.py sdist bdist_wheel

产生两个文件

dist/example_pkg-0.0.1-py3-none-any.whlexample_pkg-0.0.1.tar.gz

tar.gz 源文件
.whl 分发文件

检查打包的文件是否正常

python setup.py install  # 安装

按照使用方式导入测试,没问题后继续

上传文件

安装twine

pip install twine

上传

twine upload dist/*

没有报错就成功了

Uploading distributions to https://test.pypi.org/legacy/
Enter your username: [your username]
Enter your password:
Uploading example_pkg-0.0.1-py3-none-any.whl
100%|█████████████████████| 4.65k/4.65k [00:01<00:00, 2.88kB/s]
Uploading example_pkg-0.0.1.tar.gz
100%|█████████████████████| 4.25k/4.25k [00:01<00:00, 3.05kB/s]

安装你刚刚上传的包

pip install -i https://test.pypi.org/simple/ example_pkg

导入测试

>>> import example_pkg
>>> example_pkg.name
'example_pkg'

总结

发布项目分三步

  1. 配置setup.py文件
  2. 打包项目
  3. 发布项目
python setup.py sdist bdist_wheeltwine upload dist/*

建议

1、打包流程

打包过程中也可以多增加一些额外的操作,减少上传中的错误

# 先升级打包工具
pip install --upgrade setuptools wheel twine# 打包
python setup.py sdist bdist_wheel# 检查
twine check dist/*# 上传pypi
twine upload dist/*# 安装最新的版本测试
pip install -U example_pkg -i https://pypi.org/simple

2、关于markdown 格式的readme文件

from setuptools import setup# read the contents of your README file
from os import path
this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:long_description = f.read()setup(name='an_example_package',# other arguments omittedlong_description=long_description,long_description_content_type='text/markdown'
)

3、关于多文件打包

添加文件 MANIFEST.in,大致内容如下, 会将用到的文件都打包进来

include README.md
include requirements.txt
graft spideradmin/static
graft spideradmin/templates
global-include *.py
global-exclude *.pyc

4、常用的打包设置

setup.py 示例

# -*- coding: utf-8 -*-import iofrom setuptools import setup, find_packagesVERSION = '0.0.6'with io.open("README.md", 'r', encoding='utf-8') as f:long_description = f.read()setup(name='spideradmin',version=VERSION,description="a spider admin based scrapyd api and APScheduler",keywords='spider admin',author='Peng Shiyu',author_email='pengshiyuyx@gmail.com',license='MIT',url="https://github.com/mouday/SpiderAdmin",long_description=long_description,long_description_content_type='text/markdown',classifiers=["Programming Language :: Python :: 3","Programming Language :: Python :: 3.6"],packages=find_packages(),include_package_data=True,zip_safe=True,install_requires=["requests>=2.22.0","Flask>=1.0.3","APScheduler>=3.6.0","tinydb>=3.13.0","Flask-BasicAuth>=0.2.0"],entry_points={'console_scripts': ['spideradmin = spideradmin.run:main']}
)

参考

Packaging Python Projects

python项目打包发布总结

Python编程:twine模块打包python项目上传pypi相关推荐

  1. python模块打包上传pypi

    python模块打包上传pypi pypi注册 先在pypi.org上注册账户密码,务必牢记 包 模块尽量都保存在包中,如果要进行模块的定义,首先定义的就是包, 所以每个目录下需要一个__init__ ...

  2. python快速编程入门课后程序题答案-Python编程从零基础到项目实战 完整PPT+习题答案...

    Python编程从零基础到项目实战是一本介绍Python相关知识的Python基础教程,内容涉及算法.Python数据分析.图形处理.Web开发.科学计算.项目管理.人工智能.Python爬虫等.其中 ...

  3. python程序打包成apk_利用Gradle+Python3自动打包Android APK上传到蒲公英。

    利用Gradle+Python3自动打包Android APK上传到蒲公英. 面对每次都要打包一个版本发布给测试,都要手动打包签名~然后登录上传文件.这些繁琐的事情.于是就想到一句很经典的话,(人生苦 ...

  4. 零基础学python实战答案-Python编程从零基础到项目实战 完整PPT+习题答案

    Python编程从零基础到项目实战是一本介绍Python相关知识的Python基础教程,内容涉及算法.Python数据分析.图形处理.Web开发.科学计算.项目管理.人工智能.Python爬虫等.其中 ...

  5. python编程方式_自学python编程的方法路线

    怎么自学python编程 如何自学Python编程?一堆的Python教程却感觉无从下手呢?我想这应该是很多Python初学者正在纠结的问题. 今天想要分享给大家的是如何自学Python编程,学习这件 ...

  6. python编程在哪里写-python入门该从哪里开始?

    相信对于每个人而言,知道编程和学习编程这件事,出发点是不同的.汤哥在北京接触编程的时间是2013年,那个时候还在一个二线城市上大学,还没有这么多各种融资,各种互联网创业的氛围,大家想的更多的是一些线下 ...

  7. python编程300集免费-python 300本电子书合集

    链接: https://pan.baidu.com/s/1CNlB35ASnDNlUGNCZJbiAA 提取码: fxig Q群:592857363 更多所在 数据科学速查表 零起点Python机器学 ...

  8. 手机版python编程软件下载,手机python编程软件

    1.求 python 64位安装包下载 软件介绍: python是一款面向对象.解释型.动态数据类型的高级编程设计语言.它拥有语言上的简洁性.可读性和易维护性,在图形处理.数学处理.文本处理.系统编程 ...

  9. 树莓派python编程优点_树莓派为什么会使用python编程?为什么有时python运行效率不高?...

    在许多编程语言中,Python的语法也有很大不同.Python使用空格或缩进来分隔不同的代码块.C语言和其他语言使用花括号来区分不同的代码块,例如if语句,Python使用冒号和缩进来定义代码块.这也 ...

最新文章

  1. 克莱姆V(克莱姆相关系数、克莱姆关联系数、独立系数)
  2. Execute Process Task
  3. TensorFlow载入VGG并可视化每层
  4. 【TensorFlow】TensorFlow从浅入深系列之十一 -- 教你深入理解卷积神经网络中的卷积层
  5. I00001 杨辉三角
  6. php png 透明缩略图,php生成图片缩略图,支持png透明
  7. 大数据之 Hive 教程
  8. 使用rufus-3.8 制作启动U盘安装Windows severs 2019
  9. irq_desc操作
  10. h5加java棋牌_Html5斗地主棋牌架设Canvas实现斗地主游戏代码解析
  11. Python 给视频添加水印
  12. 华为借贴牌沃达丰闯欧洲 首款3G手机9月上市
  13. 绿地签约十家酒店项目,轻资产输出步伐再提档
  14. 我的世界服务器拔刀修复,我的世界拔刀剑怎么修复武器攻略分享
  15. 瓷砖铺贴方法_5种常见的瓷砖铺贴以及施工方法介绍
  16. 2022危险化学品生产单位安全生产管理人员考题及在线模拟考试
  17. 最新今日头条抢红包福袋脚本下载
  18. 5G NGC — 开放的 N4 接口
  19. 使用Google Maps API和google-maps-react进行React Apps
  20. 软件测试工程师的岗位职责

热门文章

  1. 《扬帆优配》北向资金新动向!增持硅片龙头,减仓“保险茅”
  2. 推广引流的核心秘诀?你知道吗?大部分人都不懂,所以只能做着苦逼搬砖的活!
  3. Cisco 交换机/路由器 ssh 配置
  4. 麒麟990怎么转鸿蒙,3个大动作!鸿蒙OS2.0首批体验出炉,麒麟990重获新生
  5. 什么是软件成分分析(SCA)安全测试技术
  6. 铁路计算机科学与技术试题,北京交通大学计算机科学与技术(铁路信息技术)专业介绍专业解读...
  7. GRANDMICRO有容微ASW3410——扩展坞/HDMI切换器 模拟开关由一级代理商KOYUELEC光与电子提供原厂技术支持和应用方案
  8. RecyclerView的高级用法——定制动画
  9. 情景剧《寝室那点事》
  10. 令容智慧燃气解决方案有效提高燃气管控