-2

Relatively simple question, how does one pip install several packages/modules within a python script? I have a script that calls on other scripts, and several people use the main script. So when someone runs it, it needs to install or update all listed packages automatically.

I tried using the solution linked but was unable to get something working.

Below code,

# Implement pip as a subprocess:
package = ['os', 'time', 'glob2', 'selenium', 'chromedriver_binary',
           'time', 'datetime']
def install(package):
    subprocess.check_call([sys.executable, '-m', 'pip', 'install', package])
Cordell Jones
  • 93
  • 1
  • 8

1 Answers1

2

You should be getting an error here, because the list items in the call to check_call should be strings, but you're passing packages, which is a list of strings. I think you want:

subprocess.check_call([sys.executable, '-m', 'pip', 'install'] + package)

Here, we're appending the package list to the arguments, so we'll end up with a command line like:

python -m pip install glob2 selenium...
larsks
  • 277,717
  • 41
  • 399
  • 399
  • I get this error, CalledProcessError: Command '['C:\\Users\\GAS02224\\Anaconda3\\python.exe', '-m', 'pip', 'install', 'os', 'time', 'glob2', 'selenium', 'chromedriver_binary', 'time', 'datetime']' returned non-zero exit status 1. – Cordell Jones Feb 17 '22 at 19:28
  • 3
    You can't `pip install` a Python builtin like `os` or `time`. – larsks Feb 17 '22 at 19:37