Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I upgrade a dependency in a Python project on Heroku

Tags:

python

pip

heroku

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?

like image 632
mjumbewu Avatar asked Feb 27 '12 19:02

mjumbewu


People also ask

How do I specify python dependencies on Heroku?

To specify Python package dependencies on Heroku via pip, add a pip requirements file named requirements.txt to the root of your repository. If you follow these simple recommendations, your application builds will be deterministic: All package versions should be explicitly specified. All secondary dependencies should be explicitly specified.

Does Heroku upgrade packages that are already in the version?

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.

How do I promote an app Using Heroku pipelines?

When using the Heroku Pipelines feature, promoting an app copies its slug to the target app and overwrites the target app’s stack to match the stack of the originating app. As such there is no need to use stack:set on the downstream (production) app directly.

How do I deploy an app to heroku-22?

Set the stack image to the Heroku-22 stack for that newly created app; Set up a new Git remote named “ heroku-22 ”. Your Git repository now contains at least two remotes: heroku, pointing to your existing production app, and heroku-22, pointing to the new app you just created.


3 Answers

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.

like image 194
CraigKerstiens Avatar answered Oct 21 '22 05:10

CraigKerstiens


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.

like image 32
dbinetti Avatar answered Oct 21 '22 04:10

dbinetti


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.

like image 20
txomon Avatar answered Oct 21 '22 04:10

txomon