访问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,并希望保留包的版本 这里:

^{pr2}$

因为您需要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-sourcesetup.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)

版本必须在自定义正则表达式的第一个组()中。在

贡献

欢迎拉取请求。如需重大更改,请先将问题打开到 讨论你想改变什么。在

请确保根据需要更新测试。在

许可证

MIT

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Scala相当于java。util。ArrayList   使用jsoup从webview解析loggedin网站   java如何在字符串资源中设置值   java需要用奇怪的模式解析XML   java Freemarker StringTemplateLoader   java通过命令行参数更改默认ant目标   java CRC32更改初始值   socket中的java J2ME IOError::open=11004   运行Mahout 0.9文本处理示例时遇到的java问题   java Apache James Spring发行版未启动   JavaQuartz 2.2。X和Spring4集成   java Jpanel使用鼠标移动事件重新绘制   java使用Ant预编译JSP的最佳方法是什么   java测试spring安全帖子。访问此资源需要完全身份验证