501

I'm using pip with a requirements file, in a virtualenv, for my Django projects. I'm trying to upgrade some packages, notably Django itself, and I'm getting an error about source code conflicts:

Source in <virtualenv>/build/Django has version 1.2.3 that conflicts with Django==1.2.4 (from -r requirements/apps.txt (line 3))

That's after updating the version number of Django from 1.2.3 to 1.2.4 in my requirements file. I'm using this command to actually do the upgrade:

pip --install --upgrade -E `<virtualenv dir`> --requirement `<requirements file`>

I can't find any flag that triggers a total package re-download. I even tried running an uninstall command first, and then the install, but no dice. Am I missing something?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
gcaprio
  • 5,067
  • 2
  • 14
  • 8
  • 1
    Please check @dr-jimbob 's answer because recent versions of pip will actually upgrade all other dependencies that the package you are upgrading depends on. – Lokesh Meher Jan 31 '18 at 09:00

10 Answers10

1414

I ran the following command and it upgraded from 1.2.3 to 1.4.0

pip install Django --upgrade

Shortcut for upgrade:

pip install Django -U

Note: if the package you are upgrading has any requirements this command will additionally upgrade all the requirements to the latest versions available. In recent versions of pip, you can prevent this behavior by specifying --upgrade-strategy only-if-needed. With that flag, dependencies will not be upgraded unless the installed versions of the dependent packages no longer satisfy the requirements of the upgraded package.

dr jimbob
  • 15,644
  • 5
  • 53
  • 74
JoeyG
  • 14,173
  • 2
  • 11
  • 3
  • 7
    This for me updated all my packaged and totally messed up everything!!!! Documents states: --upgrade all packages to the newest available version. Is this correct or did I do something wrong? – Prometheus Nov 10 '14 at 12:12
  • 4
    @OrbiterFleet I needed to update "requests" so I did `pip install Requests --upgrade` and *only* "requests" was upgraded. The description says "upgrade all *specified* packages" when I view the docs. – harperville Jan 28 '15 at 14:14
  • 37
    ATTENTION, it also **updates all dependencies** and can mess up with everything. the best option is to uninstall and reinstall the package. – marcelosalloum May 07 '15 at 13:54
  • 5
    to upgrade specific package use `pip install --upgrade django==1.4.0` – whale_steward Oct 12 '16 at 01:46
  • 1
    this should be the right answer according to question or the question itself is wrong. – int soumen Dec 01 '19 at 15:07
  • Is that the proper syntax highlight for the command lines (*not* a rhetorical question)? – Peter Mortensen Jun 27 '20 at 16:29
  • `pip install -U ` and `pip install --upgrade ` are also valid syntax – Luis_RH Sep 22 '20 at 15:48
  • If you are using `pipenv` you run the command `pipenv update PackageName` – FedericoCapaldo Mar 31 '21 at 10:19
69

First make sure you have checked the most voted answer.


I'm not sure if it's exactly your problem, but in my case, I wasn't able to upgrade Django to 1.2.4 - I was always finishing with 1.2.3 version, so I uninstalled Django with:

<virtualenv>/bin/pip uninstall Django

Then I removed <virtualenv>/build/Django directory and finally I installed the proper version with:

<virtualenv>/bin/pip install Django
Antonio
  • 17,405
  • 10
  • 78
  • 178
Marcin Świerczyński
  • 2,096
  • 21
  • 29
63

According to pip documentation example 3:

pip install --upgrade django

But based on my experience, using this method will also upgrade any package related to it. Example:

Assume you want to upgrade somepackage that require Django >= 1.2.4 using this kind of method it will also upgrade somepackage and django to the newest update. Just to be safe, do:

# Assume you want to keep Django 1.2.4
pip install --upgrade somepackage django==1.2.4

Doing this will upgrade somepackage and keeping Django to the 1.2.4 version.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
whale_steward
  • 1,474
  • 1
  • 21
  • 34
  • Good catch but your explanation is confusing since you started with `django` as a main package then in the example you use `somepackage` and then use `django` as a dependency. – Long May 22 '20 at 03:34
  • Yes it should be `pip install --upgrade django==1.2.4` without the "somepackage" – Veliko Sep 15 '20 at 08:08
39

The shortcut command for --upgrade:

pip install Django --upgrade

Is:

pip install Django -U
Aaron Lelevier
  • 16,606
  • 11
  • 62
  • 101
  • 8
    How about editing the existing answer to add this bit of information instead of an (incomplete) answer? – a1an Jun 02 '15 at 08:18
  • @a1an please suggest what extra info that you think should be added in a "suggested edit". Thanks – Aaron Lelevier Jun 02 '15 at 14:22
  • 4
    I mean you could edit the answer given by JoeyG, adding the shortcut option you provided in context there. – a1an Jun 03 '15 at 09:39
21

If you only want to upgrade one specific package called somepackage, the command you should use in recent versions of pip is

pip install --upgrade --upgrade-strategy only-if-needed somepackage

This is very useful when you develop an application in Django that currently will only work with a specific version of Django (say Django=1.9.x) and want to upgrade some dependent package with a bug-fix/new feature and the upgraded package depends on Django (but it works with, say, any version of Django after 1.5).

The default behavior of pip install --upgrade django-some-package would be to upgrade Django to the latest version available which could otherwise break your application, though with the --upgrade-strategy only-if-needed dependent packages will now only be upgraded as necessary.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
dr jimbob
  • 15,644
  • 5
  • 53
  • 74
18

If you upgrade a package, the old one will be uninstalled.

A convenient way to do this is to use this pip-upgrader which also updates the versions in your requirements.txt file for the chosen packages (or all packages).

Installation

pip install pip-upgrader

Usage

Activate your virtualenv (important, because it will also install the new versions of upgraded packages in current virtualenv).

cd into your project directory, and then run:

pip-upgrade

Advanced usage

If the requirements are placed in a non-standard location, send them as arguments:

pip-upgrade path/to/requirements.txt

If you already know what package you want to upgrade, simply send them as arguments:

pip-upgrade -p django -p celery -p dateutil

If you need to upgrade to pre-release / post-release version, add --prerelease argument to your command.

Full disclosure: I wrote this package.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Simion Agavriloaei
  • 1,981
  • 2
  • 14
  • 9
5

This solved the issue for me:

pip install -I --upgrade psutil --force

Afterwards just uninstall psutil with the new version and hop you can suddenly install the older version (:

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Eran Hpac
  • 61
  • 1
  • 2
4

Normally, pip will clean up after itself and remove the contents of the build directory. The only time it doesn't do this is if:

  1. You used the --no-install option
  2. You are using editable packages
  3. The installation was cancelled or was otherwise interrupted.

In all other cases, you shouldn't have build directory that's clogging your environment.

Burhan Khalid
  • 152,028
  • 17
  • 215
  • 255
2

Defining a specific version to upgrade helped me instead of only the upgrade command.

pip3 install larapy-installer==0.4.01 -U
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Bedram Tamang
  • 1,437
  • 12
  • 13
0

I use this:

pip3 install -r  requirements.txt  
Shahab Rahnama
  • 169
  • 2
  • 9