Pip & Git

As people commonly do, we claim requirements in requirements.txt file. When we need a library, just add a line such as library==0.1.0.

If we need to install library from Git, pip enable us to do that. pip currently supports cloning over git, git+https and git+ssh:

git+ssh://git@ghe.domain.com/org/repo.git#egg=repo
git+https://ghe.domain.com/org/repo.git#egg=repo
git+git@ghe.domain.com:org/repo.git#egg=repo

We can also passing branch names, a commit hash or a tag name as an url anchor:

git+ssh://git@ghe.domain.com/org/repo.git@0.1.0#egg=repo

We must know there are 2 types of installing mode:

  • for editable mode, by adding -e option, pip will install library by cloning project into a src directory, which means source code included.
  • for non-editable mode, the project is built locally in a temp dir and then installed normally.
-e git+ssh://git@ghe.domain.com/org/repo.git#egg=repo

Q: How to choose these modes?

A: If you have tagged git commit to a specified version, just use non-editable mode.

Normally, we will append version to egg is used by pip in its dependency logic to identify the project prior to pip downloading and analyzing the metadata. The optional “version” component of the egg name is extremely important if you want to upgrade library in non-editable mode. The format of egg is like #egg=library-0.1.0:

git+ssh://git@ghe.liwushuo.com/lws/flask-ldap.git@v0.5.0#egg=flask_ldap-0.5.0
/tmp % virtualenv venv; \
source venv/bin/activate; \
echo 'git+ssh://git@ghe.liwushuo.com/lws/flask-ldap.git@v0.2.0#egg=flask_ldap' > requirements.txt; \
pip install -r requirements.txt; \
echo 'git+ssh://git@ghe.liwushuo.com/lws/flask-ldap.git@v0.3.0#egg=flask_ldap' > requirements.txt; \
pip install -r requirements.txt
New python executable in venv/bin/python2.7
Also creating executable in venv/bin/python
Installing setuptools, pip, wheel...done.
You are using pip version 7.0.1, however version 7.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting flask-ldap from git+ssh://git@ghe.liwushuo.com/lws/flask-ldap.git@v0.2.0#egg=flask_ldap (from -r requirements.txt (line 1))
Cloning ssh://git@ghe.liwushuo.com/lws/flask-ldap.git (to v0.2.0) to /private/var/folders/2g/xtwm8ql505v4cn4wtxlr2tch0000gn/T/pip-build-vNcbl0/flask-ldap
Collecting Flask (from flask-ldap->-r requirements.txt (line 1))
Collecting python-ldap (from flask-ldap->-r requirements.txt (line 1))
Collecting msgpack-python (from flask-ldap->-r requirements.txt (line 1))
Collecting redis (from flask-ldap->-r requirements.txt (line 1))
Collecting itsdangerous>=0.21 (from Flask->flask-ldap->-r requirements.txt (line 1))
Collecting Werkzeug>=0.7 (from Flask->flask-ldap->-r requirements.txt (line 1))
Using cached Werkzeug-0.10.4-py2.py3-none-any.whl
Collecting Jinja2>=2.4 (from Flask->flask-ldap->-r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): setuptools in ./venv/lib/python2.7/site-packages (from python-ldap->flask-ldap->-r requirements.txt (line 1))
Collecting markupsafe (from Jinja2>=2.4->Flask->flask-ldap->-r requirements.txt (line 1))
Installing collected packages: itsdangerous, Werkzeug, markupsafe, Jinja2, Flask, python-ldap, msgpack-python, redis, flask-ldap
Running setup.py install for flask-ldap
Successfully installed Flask-0.10.1 Jinja2-2.7.3 Werkzeug-0.10.4 flask-ldap-0.2.0 itsdangerous-0.24 markupsafe-0.23 msgpack-python-0.4.6 python-ldap-2.4.19 redis-2.10.3
git+ssh://git@ghe.liwushuo.com/lws/flask-ldap.git@v0.3.0#egg=flask_ldap
You are using pip version 7.0.1, however version 7.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Requirement already satisfied (use --upgrade to upgrade): flask-ldap from git+ssh://git@ghe.liwushuo.com/lws/flask-ldap.git@v0.2.0#egg=flask_ldap in ./venv/lib/python2.7/site-packages (from -r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): Flask in ./venv/lib/python2.7/site-packages (from flask-ldap->-r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): python-ldap in ./venv/lib/python2.7/site-packages (from flask-ldap->-r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): msgpack-python in ./venv/lib/python2.7/site-packages (from flask-ldap->-r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): redis in ./venv/lib/python2.7/site-packages (from flask-ldap->-r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): itsdangerous>=0.21 in ./venv/lib/python2.7/site-packages (from Flask->flask-ldap->-r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): Werkzeug>=0.7 in ./venv/lib/python2.7/site-packages (from Flask->flask-ldap->-r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): Jinja2>=2.4 in ./venv/lib/python2.7/site-packages (from Flask->flask-ldap->-r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): setuptools in ./venv/lib/python2.7/site-packages (from python-ldap->flask-ldap->-r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): markupsafe in ./venv/lib/python2.7/site-packages (from Jinja2>=2.4->Flask->flask-ldap->-r requirements.txt (line 1))