Introduction
In Python the necessities.txt
file helps handle dependencies. It is a easy textual content file that lists the packages that your Python undertaking depends upon. However do you know you may also specify a direct GitHub repo as a supply in your necessities.txt
? On this Byte, we’ll discover how and why to do that.
Why specify a direct GitHub repo?
There are a number of the explanation why you would possibly wish to specify a direct GitHub repo in your necessities.txt
file. Perhaps the bundle you want is not out there on PyPI, or maybe you want a particular model of a bundle that is solely out there on GitHub (in any case, in some older packages, updates do not all the time get printed on PyPI). Or, you can be collaborating on a undertaking and wish to use the newest modifications that have not been pushed to PyPI but.
As an example, there have been a number of instances the place I wanted a function from a Python library that was solely out there within the growth department on GitHub. By specifying the direct GitHub repo in our necessities.txt
, we have been ready to make use of this function earlier than it was formally launched.
And lastly, you should utilize direct URLs as a means to make use of personal repos from GitHub.
Tips on how to Use a Direct GitHub Supply in necessities.txt
To specify a direct GitHub repo in your necessities.txt
, you will want to make use of the next format:
git+https://github.com/username/repo.git
As an example we wish to set up the newest code from the requests
library straight from GitHub. We’d add the next line to our necessities.txt
:
git+https://github.com/psf/requests.git
Then, we will set up the dependencies from our necessities.txt
as normal:
$ pip set up -r necessities.txt
You may see that pip
clones the requests
repo and installs it.
Variations of the Repo URL
There are a number of variations of the repo URL you should utilize, relying in your wants.
If you wish to set up a particular department, use the @
image adopted by the department title:
git+https://github.com/username/repo.git@branch-name
To put in a particular commit, use the @
image adopted by the commit hash:
git+https://github.com/username/repo.git@commit-hash
And naturally, one other generally used model is for personal repos. For these, you should utilize an entry token:
git+https://<token>@github.com/username/repo.git
Wait! Watch out with entry tokens, they’re much like passwords in that they provide entry to your account. Do not commit them to your model management system.
I might suggest utilizing surroundings variables to maintain them safe. When utilizing surroundings variables (i.e. ${GH_ACCESS_TOKEN}
), pip
will change it when putting in from necessities.txt.
Conclusion
With the ability to specify a direct GitHub supply in your necessities.txt
offers you extra flexibility in managing your Python undertaking’s dependencies. Whether or not you want a particular model of a bundle, wish to use a function that hasn’t been formally launched but, or are working with personal repos, this system is usually a very great tool in your growth workflow.