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).
Asked
Active
Viewed 1,199 times
2 Answers
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
-
2The 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
-
-
@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
-