16

For my (Django) project on Heroku, I updated one of the dependencies in my requirements.txt file to a newer version, and now I want Heroku to upgrade the version installed. I tried:

heroku run "pip install -r requirements.txt --upgrade -E ."

Which spits the right output to the terminal, but apparently does not actually upgrade anything; when I run:

heroku run "pip freeze -E ."

All of the package versions are the same. I even tried removing the package and that didn't work either. How do I force an upgrade of a dependency in a Python project on Heroku?

mjumbewu
  • 1,104
  • 5
  • 14
  • 28

4 Answers4

18

You should be able to upgrade it locally then re-run pip freeze. Within your requirements.txt the ==versionhere should be the version that installs each time you push.

When you run heroku run, its run in an isolated dyno that it is upgraded on then destroyed. For the change to persist it must occur during git push to be compiled into your slug.

CraigKerstiens
  • 5,906
  • 1
  • 25
  • 28
  • 2
    Hm, no luck. FMI, the package is Django. My requirements file originally just listed "Django", which installed v1.3.1. Now I want to work with the dev version, so I changed the line in requirements.txt to git+git://github.com/django/django.git#egg=django. No upgrading happened. Even tried git+git://github.com/django/django.git#egg=django==1.4b1 (the version listed in my local environment). I found http://stackoverflow.com/a/9463068/123776, but I got " ! Heroku push rejected, error fetching custom buildpack". – mjumbewu Feb 27 '12 at 21:07
  • 1
    Can you attempt it with the specific git commit hash? Within your requirements.txt it should look like git+git://github.com/django/django.git@aa4274f5716a044433b9dbfa0ad709453554e8e7 – CraigKerstiens Feb 27 '12 at 21:20
  • 2
    Actually, this appears to have worked: git+git://github.com/django/django.git@master#egg=django==1.4b1. Not sure if I did it wrong the first time. Or maybe it was the @master. But it worked. Thanks. – mjumbewu Feb 27 '12 at 21:46
  • @CraigKerstiens Fwiw, this appears to be a bug in heroku's pip implementation. If you go from a pypi version of a package (e.g. django or django==1.5.1) to a github url, like OP did, heroku's pip effectively ignores it. I have run into this problem several times when having a pypi package in my requirements.txt, but having to fork it to fix a bug and get it deployed right away. And it doesn't update the package to the github url and I have to start searching stack overflow for a work-around... looks like #egg= may be the hack i'm looking for, but a fix would be nice. – B Robster Feb 09 '14 at 05:03
  • 3
    Looking at this, its just the way pip works, nothing specific to Heroku. Options are to use the #egg=== or to force heroku to re-install the environment with this trick: http://stackoverflow.com/a/15152659/652693 – B Robster Feb 09 '14 at 06:14
  • Heroku's feature here now works as advertised, you just have to leave off the #egg fragment entirely as specified here: https://devcenter.heroku.com/articles/python-pip – Alper Mar 06 '18 at 19:49
9

Quick update on this that there are now utils to accomplish this function.

https://github.com/heroku/heroku-repo

Howto

  1. Install the plugin in your Heroku toolbelt

    heroku plugins:install https://github.com/heroku/heroku-repo.git

  2. Clear the Heroku cache for your app (effectively removing all packages installed by pip)

    heroku repo:purge_cache -a <APPNAME>

    from the docs: This will delete the contents of the build cache stored in the repository. This is done inside a run process on the application

  3. Rebuild

    You can now push as normal.
    Currently pushing seems to be the only way to cause a rebuild, see Recompile Heroku slug without push or config change here on StackOverflow for more info.

Community
  • 1
  • 1
dbinetti
  • 215
  • 3
  • 9
  • Please give some inline explanation as well. – László Papp Feb 25 '14 at 04:48
  • 1
    Sorry I missed this. If you install the tools `heroku plugins:install https://github.com/heroku/heroku-repo.git` then you'll have access to the command `heroku repo:purge_cache -a appname`. Per the docs, " This will delete the contents of the build cache stored in the repository. This is done inside a run process on the application" Then you can push as normal, or use `heroku repo:rebuild -a appname` to rebuild immediately. – dbinetti Jul 29 '14 at 14:49
7

I wanted to submit my answer just in case someone faces the same.

Heroku doesn't upgrade packages that are already in the version (which makes sense), however it fails to upgrade a package when installing from source, even if it's a different commit.

The solution I found is to force update by using a post-compile hook with pip install --upgrade -r requirements.txt. Because the rest of the packages are pinned, it only affects source packages.

txomon
  • 642
  • 1
  • 6
  • 21
0

If for some reason it is still not updating, one thing you might try is deleting the dependency, pushing to git heroku master, and then re-adding the dependency with the right version and pushing again.

moto
  • 946
  • 10
  • 27