5

I develop a Python/Django application, which runs from a virtual environment (created by virtualenv).

When the virtual environment is created, the global version of pip is copied to the newly created environment by default, which might be quite outdated (for example, version 1.5.4 from python-pip package on Ubuntu 14.04).

To avoid manual pip upgrades, it sounds like a good idea to pin the pip version in requirements.txt file, for instance by adding the following line:

pip==8.1.2

Specifying the pip version there will also allow to upgrade pip in all the managed application environments (local, dev, production) by changing the line in the requirements file.

Does this sound like a good practice? Is there anything that can go wrong with this approach?

tonyo
  • 433
  • 7
  • 14

2 Answers2

11

Please note that pip version listed in requirements.txt is going to be installed along with other requirements. So all requirements are going to be installed by old version of pip and the version specified in requirements.txt will be available afterwards.

I always do:

virtualenv /path/to/my/desired/venv/
source /path/to/my/desired/venv/bin/activate
pip install -U pip
pip install -r requirements.txt
ElmoVanKielmo
  • 10,907
  • 2
  • 32
  • 46
  • Good point. Looks like it makes sense to do both, then: install the modern pip version right after creating the virtual environment, and specify it in requirements.txt for documentation/later-upgrades purposes. – tonyo Sep 19 '16 at 11:01
  • I wouldn't specify it in requirements.txt - it's a false requirement in the first place. Is it really the case that your software won't install correctly with older pip? I really don't think so. – ElmoVanKielmo Sep 20 '16 at 09:48
  • @ElmoVanKielmo new requirements file formats such as ~= are not backwards compatible so yes there are cases where our software won't install correctly with older pip – theannouncer Dec 13 '18 at 20:40
0

What you experience is caused by an old version of python-virtualenv delivered with Ubuntu 14.04. You should remove the Ubuntu package and install via pip:

sudo pip install virtualenv

Then make sure you have the newest pip installed as well.

sudo pip install -U pip

And you should get that version installed in new virtual environments.

Klaus D.
  • 13,874
  • 5
  • 41
  • 48
  • If `pip` was installed via `python-pip`, `sudo pip install -U pip` will not work. It will tell you: `Not uninstalling pip at /usr/lib/python2.7/dist-packages, owned by OS` – tonyo Sep 19 '16 at 11:41
  • Then, uninstall it via `apt-get`, get https://bootstrap.pypa.io/get-pip.py and `sudo python get-pip.py` or `sudo python3 get-pip.py`. – Klaus D. Sep 20 '16 at 02:46