Example use of commands
suggest changeimport 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 the parameters you need are required, so both pip.main(['freeze']) and pip.main(['freeze', '', '']) are acceptable. pip.main warns that this wrapper will fail in a future pip.
Batch install
It is possible to pass many package names in one call, but if one install/upgrade fails, the whole installation process stops and ends with status ‘1’. pip.get_installed_distributions is not provided by current pip, so the next call raises AttributeError. The sample is left as written.
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 the installation in a loop. installed is defined only by the previous sample. The loop also raises TypeError: i.key is a string, and a list cannot be concatenated with a string.
for i in installed:
pip.main(['install']+i.key+['--upgrade'])
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents