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'])
Try pip.main(['install', '--upgrade', package]).
"--upgrade" is a separate command-line argument, so you need to pass it separately to main.
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.
from subprocess import call
my_packages = ['apscheduler', 'beautifulsoup4', 'gdata']
def upgrade(package_list):
call(['pip', 'install', '--upgrade'] + package_list)
Notes:
pip module (subprocess calls the pip binary found in its path)shell=True in call() (and other subprocess 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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With