David Watson
David Watson

Reputation: 3432

how to pip uninstall with virtualenv on heroku cedar stack?

I tried to uninstall a module on heroku with:

heroku run bin/python bin/pip uninstall whatever

Pip shows the module in the /app tree then claims to have uinstalled the module, but running the same command again shows it installed in the same location in the /app tree.

Is there a way to get pip uinstall to succeed?


Heroku run instantiates a new dyno and runs the command specified in that dyno only. Dynos are ephemeral which is why the results of the pip uninstall don't stick.

Upvotes: 22

Views: 6364

Answers (4)

Andrey Martyanov
Andrey Martyanov

Reputation: 131

By default virtualenv is cached between deploys.

To avoid caching of packages you can run:

heroku config:add [email protected]:heroku/heroku-buildpack-python.git#purge

That way everything will be built from scratch after you push some changes. To enable the caching just remove the BUILDPACK_URL config variable.

Now to uninstall specific package(s):

  1. Remove the corresponding record(s) from the requirements.txt;
  2. Commit and push the changes.

Thanks to Lincoln from Heroku Support Team for the clarifications.

Upvotes: 10

Maxime R.
Maxime R.

Reputation: 10001

Updated 2013-09-30: the current way to clear the virtualenv seems to specify a different python runtime version in runtime.txt as stated on Github and in the Heroku's devcenter reference.

Be aware that Heroku currently "only endorses and supports the use of Python 2.7.4 and 3.3.2" so unless your application supports both Python 2.7.4 and 3.3.2, you may want to test it with the runtime you'll want to switch to (currently available at http://envy-versions.s3.amazonaws.com/$PYTHON_VERSION.tar.bz2, though it shouldn't be an issue to switch e.g. between 2.7.4 and 2.7.3 in most cases).

Thanks @Jesse for your up-to-date answer and to the commenters who made me aware of the issue.


Was up-to-date in ~november 2012 (I haven't since updated the linked buildpack, my pull request was closed and the CLEAN_VIRTUALENV feature was dropped at some point by the official buildpack):

As David explained, you cannot pip uninstall one package but you can purge and reinstall the whole virtualenv. Use the user-env-compile lab feature with the CLEAN_VIRTUALENV option to purge the virtualenv:

heroku labs:enable user-env-compile
heroku config:add CLEAN_VIRTUALENV=true

Currently this won't work because there is a bug. You'll need to use my fork of the buildpack until this get fixed upstream (pull request was closed) :

heroku config:add BUILDPACK_URL=https://github.com/blaze33/heroku-buildpack-python.git

Now push your new code and you'll notice that the whole virtualenv gets reinstalled.

Andrey's answer no longer works since March 23 2012. The new style virtualenv commit moved the virtual env from /app to /app/.heroku/venv but the purge branch wasn't updated to catch up so that you end up with a virtualenv not being in PYTHONHOME.

To avoid reinstalling everything after each push, disable the option:

heroku labs:disable user-env-compile
heroku config:remove CLEAN_VIRTUALENV BUILDPACK_URL

Upvotes: 32

littlepea
littlepea

Reputation: 1034

I've created some fabfile recipes for Maxime and Jesse answers that allow re-installing the requirements with one fab command: https://gist.github.com/littlepea/5096814 (look at the docstrings for explanation and examples).

For Maxime's answer I've created a task 'heroku_clean' (or 'hc'), it'll look something like that:

fab heroku_clean

Or using an alias and specifying a heroku app:

fab hc:app=myapp

For Jesse's answer I've created a task 'heroku_runtime' (or 'hr'), it sets heroku python runtime and commits runtime.txt (also creates it if it didn't exist):

fab heroku_runtime:2.7.2

If runtime version is not passed it will just toggle it between 2.7.2 and 2.7.3, so the easiest way to change and commit the runtime is:

fab hr

Then you can just deploy (push to heroku origin) you app and the virtualenv will be rebuilt. I've also added a 'heroku_deploy' task ('hr') that I use for heroku push and scale that also can be user together with 'heroku_runtime' task. This is my preferred method of deploying and rebuilding virtualenv - everything happens in one command and I can choose when to rebuild it, I don't like to do it every time like Maxime's answer suggest because it can take a long time:

fab hd:runtime=yes

This is an equivalent of:

fab heroku_runtime
fab heroku_deploy

Upvotes: 0

Jesse
Jesse

Reputation: 373

There is now a simpler way to clear the pip cache. Just change the runtime environment, for example from 'python-2.7.3' to 'python-2.7.2', or vice versa.

To do this add a file called runtime.txt to the root of your repository that contains just the runtime string (as show above) in it.

For this to work you need to have turned on the Heroku labs user-env-compile feature. See https://devcenter.heroku.com/articles/labs-user-env-compile

Upvotes: 22

Related Questions