Search code examples
pythonpip

Pip Install a List of Packages


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])

Solution

  • 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...