将版本嵌入python包的标准方法? [英] Standard way to embed version into python package?

查看:155
本文介绍了将版本嵌入python包的标准方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在将版本字符串与python软件包相关联的标准方法,使得我可以执行以下操作?

Is there a standard way to associate version string with a python package in such way that I could do the following?

import foo
print foo.version

我想像有某种方法可以检索数据而无需任何额外的硬编码,因为已经在setup.py中指定了次要/主要字符串.我发现的替代解决方案是在foo/__init__.py中包含import __version__,然后由setup.py生成__version__.py.

I would imagine there's some way to retrieve that data without any extra hardcoding, since minor/major strings are specified in setup.py already. Alternative solution that I found was to have import __version__ in my foo/__init__.py and then have __version__.py generated by setup.py.

推荐答案

不是直接回答您的问题,但您应该考虑将其命名为__version__,而不是version.

Not directly an answer to your question, but you should consider naming it __version__, not version.

这几乎是一个准标准.标准库中的许多模块都使用__version__,并且

This is almost a quasi-standard. Many modules in the standard library use __version__, and this is also used in lots of 3rd-party modules, so it's the quasi-standard.

通常,__version__是一个字符串,但是有时它也是一个浮点数或元组.

Usually, __version__ is a string, but sometimes it's also a float or tuple.

如S.Lott(谢谢!)所述, PEP 8 明确地说:

as mentioned by S.Lott (Thank you!), PEP 8 says it explicitly:

模块级别的个人名称

模块级"dunders"(即带有两个前导和两个尾随的名称 下划线),例如__all____author____version__等. 应该放置在模块文档字符串之后,但是在任何导入之前 语句,__future__导入除外.

Module Level Dunder Names

Module level "dunders" (i.e. names with two leading and two trailing underscores) such as __all__, __author__, __version__, etc. should be placed after the module docstring but before any import statements except from __future__ imports.

您还应该确保版本号符合 PEP 440中描述的格式( PEP 386 此标准的先前版本).

You should also make sure that the version number conforms to the format described in PEP 440 (PEP 386 a previous version of this standard).

这篇关于将版本嵌入python包的标准方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆