Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 233
David Watson Avatar asked Jan 20 '12 07:01

David Watson


People also ask

How do I uninstall a package with PIP?

To use pip to uninstall a package locally in a virtual environment: Open a command or terminal window (depending on the operating system) cd into the project directory. pip uninstall <packagename>


3 Answers

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
like image 72
Maxime R. Avatar answered Nov 04 '22 15:11

Maxime R.


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

like image 33
Jesse Avatar answered Nov 04 '22 17:11

Jesse


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.

like image 10
Andrey Martyanov Avatar answered Nov 04 '22 17:11

Andrey Martyanov