如何创建Python程序包,Python程序包结构详解(超级详细)

本文详细讲解了如何创建和管理Python程序包,包括setup.py脚本、setup.cfg配置、MANIFEST.in文件、元数据、trove分类器以及常见的打包模式,如自动包含版本字符串、README文件处理和依赖管理。了解这些内容将帮助开发者更好地组织和分发Python代码。

我们知道,组织大型应用的代码的最简单方法,通常就是将其分成几个包,这使得代码更加简单,也更容易理解、维护和修改,同时还可以使每个包的可复用性最大化,它们的作用就像组件一样。

setup.py脚本文件

对于一个需要被分发的包来说,其根目录包含一个 setup.py 脚本,它定义了 distutils 模块中描述的所有元数据,并将其合并为标准的 setup() 函数调用的参数。

虽然 distutils 是一个标准库模块,但建议读者使用 setuptools 包来代替,因为它对标准的 distutils 做了一些改进。

因此,这个文件的最少内容如下:

1.  from setuptools import setup
2.  setup(
3.  name='mypackage',
4.  )

其中,name 给出了包的全名。

另外,该脚本提供了一些命令,可以用 --help -commands 选项列出这些命令:

$Python setup.py --help-commands
tandard commands:
  build             build everything needed to install
  clean             clean up temporary files from ‘build’ command
  install           install everything from build directory
  sdist             create a source distribution (tarball, zip file)
  register          register the distribution with the PyP
  bdist             create a built (binary) distribution
  check             perform some checks on the package
  upload            upload binary package to PyPI

Extra commands:
  develop           install package in ‘development mode’
  alias             define a shortcut to invoke one or more commands
  test              run unit t#sts after in-place build
  bdist_wheel       create a wheel distribution

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts】…]
  or: setup.py --help [cmd1 end2 …]
  or: setup.py --help-commands
  or: setup.py cmd --help

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值