访问pep621style项目的Python代码的项目版本
single-source的Python项目详细描述
单一来源:只有一个事实
single-source
helps to reduce the entropy in your Python project by keeping single source of truth.
这个库的目标是希望 版本、名称等的真实来源之一
目前,库为包版本提供了单点。在
它支持python3.6+。在
快速入门
# root_package/__init__.pyfrompathlibimportPathfromsingle_sourceimportget_version__version__=get_version(__name__,Path(__file__).parent.parent)
问题的根源
您使用现代的pyproject.toml
,并希望保留包的版本
这里:
因为您需要Python代码中的版本,所以可以如下复制:
# modern_project/__init__.py__version__="0.1.0"# modern_project/version.pyversion="0.1.0"
安装
您可以通过pip安装single-source
pip3 install single-source
或通过poetry
poetry add single-source
图书馆也可作为 a conda package英寸 conda-forge频道
conda install single-source --channel conda-forge
高级用法
更改默认值
如果无法从包元数据或
没有pyproject.toml项目get_version
返回""
-默认为空字符串。
您可以通过提供一个值作为default_return
关键字参数来更改此值。在
frompathlibimportPathfromsingle_sourceimportget_versionpath_to_pyproject_dir=Path(__file__).parent.parent__version__=get_version(__name__,path_to_pyproject_dir,default_return=None)
引发异常
您可能需要引发异常,以防包的版本 尚未找到。在
frompathlibimportPathfromsingle_sourceimportget_version,VersionNotFoundErrorpath_to_pyproject_dir=Path(__file__).parent.parenttry:__version__=get_version(__name__,path_to_pyproject_dir,fail=True)exceptVersionNotFoundError:pass
不仅如此pyproject.toml项目
即使仍然存储库的版本,也可以使用single-source
在setup.py
或任何其他utf-8
编码的文本文件中。在
First, try without custom
regex
, probably it can parse the version
如果默认的内部regex
在文件中找不到版本,
您只需要提供一个自定义的regex
到{
fromsingle_sourceimportget_versioncustom_regex=r"\s*version\s*=\s*[\"']\s*([-.\w]{3,})\s*[\"']\s*"path_to_file="~/my-project/some_file_with_version.txt"__version__=get_version(__name__,path_to_file,version_regex=custom_regex)
版本必须在自定义正则表达式的第一个组()
中。在
贡献
欢迎拉取请求。如需重大更改,请先将问题打开到 讨论你想改变什么。在
请确保根据需要更新测试。在
许可证
- 项目
标签: