Python Language Usage of "pip" module: PyPI Package Manager Example use of commands

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

import pip

command = 'install'
parameter = 'selenium'
second_param = 'numpy' # You can give as many package names as needed
switch = '--upgrade'

pip.main([command, parameter, second_param, switch])

Only needed parameters are obligatory, so both pip.main(['freeze']) and pip.main(['freeze', '', '']) are aceptable.

Batch install

It is possible to pass many package names in one call, but if one install/upgrade fails, whole installation process stops and ends with status '1'.

import pip

installed = pip.get_installed_distributions()
list = []
for i in installed:
    list.append(i.key)

pip.main(['install']+list+['--upgrade'])

If you don't want to stop when some installs fail, call installation in loop.

for i in installed:
        pip.main(['install']+i.key+['--upgrade'])


Got any Python Language Question?