我的博客

打包和发布python包

目录
  1. 项目结构
  2. setup.py 详解
  3. 打包和发布

pypi 的官方教程更详细的官方文档

我总结的大致的步骤

项目结构

  1. 根目录下建一个 src 文件夹,里面放 python 代码
  2. 根目录下建一个 setup.py 项目的信息,包含:
    1. 包名、作者、网站等
    2. 项目依赖
    3. 项目源码目录,就是 src
    4. 命令,可以随着安装直接添加到系统的 path 中

setup.py 详解

我这还不是最详细,但是比较全了,最详细看官方文档。

下面例子来自于

https://github.com/sxwxs/buptGateway

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from setuptools import setup, find_packages
from os import path

here = path.abspath(path.dirname(__file__))

# Get the long description from the README file
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()

setup(

name='buptgw',

version='0.0.1',

description='login to gw.bupt.edu.cn to access the Internet',

long_description=long_description,
long_description_content_type='text/markdown',

url='https://github.com/sxwxs/buptGateway', # 项目官网

author='sxwxs',

author_email='sxwxs@msn.com',

classifiers=[ # 这里的东西必须从 https://pypi.org/classifiers/ 选,不在这选传不上去
'Development Status :: 4 - Beta',

'Intended Audience :: End Users/Desktop',
'Topic :: Utilities',

'License :: OSI Approved :: MIT License',

'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
],

keywords='bupt network gateway login',

package_dir={'': 'src'}, # 源码目录

packages=find_packages(where='src'),

python_requires='>=3.5',

install_requires=['requests'],

entry_points={
'console_scripts': [ # 同时安装命令到系统 path 路径
'buptgw=buptgw:main', # 命令是 buptgw,执行 buptgw 包里的 main 函数
],
},

project_urls={
'Bug Reports': 'https://github.com/sxwxs/buptGateway/issues',
'Funding': 'https://i.loli.net/2019/12/28/pv8PUd4eKGyNDiV.png',
# 'Say Thanks!': '',
'Source': 'https://github.com/sxwxs/buptGateway',
},
)

打包和发布

注册帐号 https://pypi.org/

安装工具

1
2
pip install twine
pip install wheel

打包

1
2
python setup.py sdist
python setup.py bdist_wheel

上传

1
twine upload dist/*

评论无需登录,可以匿名,欢迎评论!