我们知道,组织大型应用的代码的最简单方法,通常就是将其分成几个包,这使得代码更加简单,也更容易理解、维护和修改,同时还可以使每个包的可复用性最大化,它们的作用就像组件一样。
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
实际的命令列表更长,而且会根据 setuptools 的可用扩展而变化。这里所列举的都是相对来说比较重要的,且和本节相关的命令。
Standard commands(标准命令)是 distutils 提供的内罝命令,而 Extra commands(额外命令)则是由诸如 setuptools 这样的第三方包或任何其他定义并注册一个新命令的包所创建的。由另一个包注册的一个额外命令就是 wheel 包提供的 bdist_wheel。
setup.cfg文件
setup.cfg 文件包含 setup.py 脚本命令的默认选项。如果构建和分发包的过程更加复杂,并且需要向 setup.py 命令中传入许多可选参数,那么这个文件非常有用。
读者可以按项目将这些默认参数保存在代码中,这将使整个分发流程独立于项目之外,也能够让包的构建方式与向用户和其他团队成员的分发方式变得透明。最后,如果你的时间不是很紧张,并且又想快速的python提高,最重要的是不怕吃苦,建议你可以架群:【832357663】 ,那个真的很不错,很多人进步都很快,需要你不怕吃苦哦!大家可以去添加上看一下~
setup.cfg 文件的语法与内置 configparser