Ramkumar
Ramkumar

Reputation: 460

Uninstall and re-install pip package from python module

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 :)

Upvotes: 8

Views: 18695

Answers (2)

AxelWass
AxelWass

Reputation: 1341

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'])

Upvotes: 2

minopret
minopret

Reputation: 4806

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.

Upvotes: 7

Related Questions