I'm having fun with python packaging today. i want to populate the __version__ keyword in my package, but only when it's pip install'd. if I'm using it locally, then I want the version to come from git describe. So what i have is this:

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
        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

which works great. But I want to make sure __version__.py only exists on released versions, not in my working repo--and I don't want to risk having a __version__.py leak into my development workspace.

So at this point, I'm researching ways to create a file in setup.py that will only exist in the distrubuted version of my package. Any have any tips or warnings as i do this?

  • setup.py of which packaging tool? py2exe? pyinstaller? cxfreeze? I use to package extra files myself when I create a .zip archive. In that case, I add a buffer to the archive where the file is supposed to be without creating that file on disk, that solves your problem :) – Jean-François Fabre Jun 12 at 13:08
  • oh, i definitely need this to be automated. while easy to add the file manually, it's time consuming and error prone – TinyTheBrontosaurus Jun 12 at 13:25
  • my script is a python script, so it's automated. I wanted to know which setup you're refereing to. Can you show your setup code? – Jean-François Fabre Jun 12 at 13:35
  • i simply call python setup.py sdist bdist_wheel – TinyTheBrontosaurus Jun 12 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.