3

Installing a package from GitHub in pip, is it possible to get the commit version and date that I am installing? (this may be useful from debugs and tests control).

hildogjr
  • 754
  • 2
  • 6
  • 17
  • Possible duplicate of [How can I know which commit was used when installing a pip package from git?](https://stackoverflow.com/questions/45681223/how-can-i-know-which-commit-was-used-when-installing-a-pip-package-from-git) – phd Apr 22 '18 at 23:43

2 Answers2

3

If you are installing in an editable way (-e git+...) you can use the git cli to query the information directly.

For example:

$ pip install -e 'git+https://github.com/pre-commit/pre-commit#egg=pre-commit'
...
$ python
...
>>> import os
>>> import subprocess
>>> import pre_commit
>>> pre_commit.__file__
'/tmp/test/venv/src/pre-commit/pre_commit/__init__.py'
>>> subprocess.check_output(('git', '-C', os.path.dirname(pre_commit.__file__), 'log', '-1', '--format=%H %cd'))
b'834ed0f229a39c986b241374f6d338632e003b5f Sat Mar 17 20:40:02 2018 -0700\n'

This abuses the fact that when you install a git repository in an "editable" way that pip clones the repository and keeps it around at $PREFIX/src (in my case ./venv/src). Note that without --editable pip will only clone the repository temporarily and the git revision history will be inaccessible.

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
2

If you download a git project of any kind you can use git commands to view the history. For instance git log HEAD~1..HEAD would give you information on the most recent commit. If you wanted to just print the abbreviated commit hash and date you could do something like

git log HEAD~1..HEAD --format="%h %ad"
Conner
  • 30,144
  • 8
  • 52
  • 73
  • 2
    The question is about [tag:pip] which clones a repo, installs a package from it and removes the repo. Hard to run `git` commands after the repo is gone. – phd Apr 22 '18 at 23:34
  • BTW, `git log HEAD~1..HEAD` can be spelled much nicer as `git show`. – phd Apr 22 '18 at 23:35
  • @phd You can use pip to install packages directly cloned from github. It doesn't necessarily always remove the repo. `git show` is not the same as `git log`, and I suppose the author found some use in the answer if he accepted it. Nevertheless, points well taken. – Conner Apr 23 '18 at 03:02
  • Probably meant `git log -1`, ie: `git log -1 --format="%h %ad"` – Efren Feb 07 '21 at 07:35