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

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

被折叠的 条评论
为什么被折叠?



