My question relates to this question: Installing python module within code, but involves upgrading the module.
I've tried
packages=['apscheduler','beautifulsoup4','gdata']
def upgrade(packages):
for package in packages:
pip.main(['install --upgrade', package])
and
def upgrade(packages):
for package in packages:
pip.main(['install', package + ' --upgrade'])
BrenBarn's answer matches the style of OP's code, but Richard Wheatley's answer is closer to my code.
Richard's answer has a couple of issues I'd like to fix, though, and I didn't want to edit his because the differences are reasonably significant.
Notes:
pip
module (subprocess
calls thepip
binary found in its path)shell=True
incall()
(and othersubprocess
functions) should be be avoided, as it puts responsibility on the programmer to protect against shell injection vulnerabilities. (Not really an issue here, but good as a general rule.)pip install
accepts an arbitrary number of package names, and a single use of--upgrade
will apply to all of them.Try
pip.main(['install', '--upgrade', package])
."--upgrade" is a separate command-line argument, so you need to pass it separately to
main
.I now this is an old thread but I have come across it many times and I think I should give another solution since I've used this one. To me this is more user-friendly.
After reading the following Actual meaning of 'shell=True' in subprocess post, it appears that the shell=True command was not needed and is probably not the best idea.
import pip
is not needed since I do not callpip.main
as was seen in some previous answers.So the new code could be: from subprocess import call
As for the other answers, I prefer passing the specific string and loop. You will not get a performance hit or gain using either method.