DEV Community

Cover image for Pip Install a Git Repository
Fredrik Sjöstrand
Fredrik Sjöstrand

Posted on • Updated on • Originally published at fronkan.hashnode.dev

Pip Install a Git Repository

If you would like to install the latest version of flask, you would probably use pip install flask. While this gives you the latest released version, it doesn't contain the latest commit to the repository. At least not most of the time. Sooo, what if I want the latest commit? Maybe some new bugfix or feature has been added which I need for my project. Let's look at how you can get the latest commit made to flask or any other open-source package.

You could clone the repository and install it using pip locally:

$ git clone https://github.com/pallets/flask.git
$ pip install ./flask
Enter fullscreen mode Exit fullscreen mode

However, this method creates a dependency on local files, and creating a requirements.txt you could share is impossible. Using pip freeze it would look like this for flask:

Flask @ file:///C:/Users/Fronkan/my_project/flask
Enter fullscreen mode Exit fullscreen mode

Fear not, there is a better way of doing this! You can use pip to install directly from a git repository. To install flask the shortest version of the command is pip install git+https://github.com/pallets/flask.git.
This will install whatever is on the default branch of the project. Freezing this gives us:

Flask @ git+https://github.com/pallets/flask.git@bdf7083cfdeef0e0bdd0bf6d4c23b26c92b52d95
Enter fullscreen mode Exit fullscreen mode

Notice how pip has stored the specific commit hash used when installing. This means you can share this with others and they will, for sure, get the same version as you!

As I said, that is the short version of the command. This corresponds to pip install git+https://github.com/pallets/flask.git@master, as master is the default branch of the flask repository. The reason I show you this is because we can change master to any other branch in the repository. Actually, we can use either a branch, commit hash, tag name, or git ref. This gives you a lot of control over what to install from the repository.

If you follow the pip documentation you would run the command as: pip install git+https://github.com/pallets/flask.git@master#egg=flask. The #egg=flask part tells pip which package it is installing before downloading and parsing the metadata and is used for the dependency logic. This seems to be the, by pip, recommended way of doing things.

Pip also supports other version control systems like Mercurial and SVM. If you use these instead of git or just want to read up on pips support for version control systems in general, check out the docs right here.

This isn't something you will use every day, I have used it once. I needed a version of a package available on the master branch which wasn't yet released, like with this flask example. However, you could also use this to install packages that aren't available on PyPI at all, for example, an internal project at work. I hope you found this as interesting as I did and if you see any other uses for this, please leave a comment!😄

Top comments (2)

Collapse
 
gijsschot profile image
gijsschot

Would you happen to know why

pip install git+ssh://git@github.com/FridoF/PyTomPrivate.git@installation#egg=pytom --user

does not create the requested executables, whereas cloning myself and then running

pip install . --user

does create the executables.

From the log I see that the wheel is created, only the installation to the lib and bin fails or is not doing what I would expect.

Any suggestions are welcome.

Collapse
 
fronkan profile image
Fredrik Sjöstrand

Unfortunately I am not sure. I noticed that the public version of this repository stated you should use git clone --recursive, so I guess it has submodules or something. Maybe this could cause the issue?

But I also noticed it didn't have a setup.py so I don't know how much the public and private repo has diverged.