2

I have a library of functions (krypton) which I have recently moved into its own Python 3.7 site package (in C:\ProgramData\Anaconda3\Lib\site-packages\krypton). I would like to keep track of the version number of the package using git tags, as it forms part of the output when the package executes. I am using GitPython to do this.

So I need to find the directory in which the krypton code is located in a reliable way in order to pass it to the GitPython Repo() method, as otherwise the CWD from which the script which imports would be used.

What I have so far is

import krypton
directory = os.path.split(krypton.__file__)[0]

repo = git.Repo(directory)
__sha__ = repo.head.object.hexsha
tags = repo.tags
if tags:
    __version__ = repo.tags[-1].name
else:
    __version__ = "unknown"

So I am importing krypton.py within itself. Which doesn't feel right, but which seems to work OK. is there a more elegant/reliable/Pythonic way I should do this?

TimGJ
  • 1,584
  • 2
  • 16
  • 32

1 Answers1

0

I believe you can skip the re-import by using inspect to get the path of the library itself. I found some stuff in this answer.

Aaron D. Marasco
  • 6,506
  • 3
  • 26
  • 39