Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uninstall and re-install pip package from python module

Tags:

python

pip

I have uninstalled package 'setuptools' from python using following pip function

import pip
pip.main(['uninstall','--yes','setuptools'])

When, I tried to re-install the same package again using below command, it throws the following error message

pip.main(['install','setuptools'])

Error:

Requirement already satisfied (use --upgrade to upgrade): setuptools in c:\\python27\\lib\\site-packages

Is there any option to overcome this ? Thanks in advance :)

like image 428
Ramkumar Avatar asked Aug 27 '15 17:08

Ramkumar


2 Answers

Yes, --ignore-installed. For more info: pip install --help which explains:

-U, --upgrade               Upgrade all specified packages to the newest
                            available version. This process is recursive
                            regardless of whether a dependency is already
                            satisfied.
--force-reinstall           When upgrading, reinstall all packages even if
                            they are already up-to-date.
-I, --ignore-installed      Ignore the installed packages (reinstalling
                            instead).

Besides, I tried it with Python 3.4. Of the above options, only pip install --ignore-installed would install a previously installed package.

like image 92
minopret Avatar answered Nov 02 '22 19:11

minopret


Uninstall won't take effect until next time python is started. So to uninstall and reinstal what you could do is divide your code into 2 files.

File "main.py":

import pip
import os

pip.main(['uninstall','--yes','setuptools'])
os.system('python install_setuptools.py')

File "install_setuptools.py":

import pip

pip.main(['install','setuptools'])
like image 37
AxelWass Avatar answered Nov 02 '22 19:11

AxelWass