在阅读其他 Python 模块时,我看到很多人经常在他们的源文件中包含全局变量(甚至在 PEP3001 中提到__version__
过)。我想用一组合理的这些变量来记录我的代码。通常可能包含的全局变量列表是什么?__author__
问问题
532 次
2 回答
2
这些全局变量没有特定的标准 - 正如您链接的 PEP 中所述,它们试图实现标准,但尚未以任何单一形式被普遍接受。
真正的标准是 PyPI 元数据,它在setup.py
文件中为您的模块指定,使用distutils
(或兼容的接口)。这是包装教程中的示例:
from distutils.core import setup
setup(
name='TowelStuff',
version='0.1.0',
author='J. Random Hacker',
author_email='jrh@example.com',
packages=['towelstuff', 'towelstuff.test'],
scripts=['bin/stowe-towels.py','bin/wash-towels.py'],
url='http://pypi.python.org/pypi/TowelStuff/',
license='LICENSE.txt',
description='Useful towel-related stuff.',
long_description=open('README.txt').read(),
install_requires=[
"Django >= 1.1.1",
"caldav == 0.1.4",
],
)
于 2013-01-21T17:34:48.507 回答
1
使用distutils
(或超集setuptools
)来提供有关您的项目的元数据。
特别是在使用setuptools
时,元数据可以通过pkg_resources
模块发现和重用。
对于全局变量,例如 没有标准__version__
,甚至对于 Python stdlib 也没有,这就是为什么在 Python 3 的 stdlib 中提供此元数据的努力没有取得任何成果。
我可以推荐Python Packaging User Guide作为如何正确打包项目的入门指南。
于 2013-01-21T17:35:57.997 回答