简体   繁体   中英

How do I upgrade a dependency in a Python project on 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?

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.

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.

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.

如果由于某种原因它仍然没有更新,你可能尝试的一件事是删除依赖项,推送到git heroku master,然后用正确的版本重新添加依赖项并再次推送。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM