首页 > 解决方案 > 在 setup.py 期间创建临时文件

问题描述

我今天玩 python 打包很开心。我想__version__在我的包中填充关键字,但只有当它是 pip install'd 时。如果我在本地使用它,那么我希望版本来自git describe. 所以我所拥有的是:

def get_version():
    """
    Get the version for the ground station. If the __version__.py exists, it uses the version in there. Otherwise
    it uses the git describe version
    """
    try:
        from . import __version__
        return __version__.__version__
    except ImportError:
        return get_git_tag()


def get_git_tag():
    """Return string of latest (annotated) git tag"""
    version = subprocess.check_output(["git", "describe", "--dirty", "--long"]).decode()
    version = version.rstrip()
    return version

效果很好。但我想确保__version__.py只存在于已发布的版本中,而不是存在于我的工作存储库中——而且我不想冒险__version__.py泄露到我的开发工作区中。

所以在这一点上,我正在研究在 setup.py 中创建一个文件的方法,该文件只存在于我的包的分布式版本中。我这样做时有任何提示或警告吗?

标签: pythonpython-3.xpackaging

解决方案


推荐阅读