简体   繁体   中英

How do I make a install.py to install several packages with Python?

I am enrolled in a machine learning competition and, for some reason, the submission is not a CSV file, but rather the code in Python. In order to make it run, they asked the participants to create another file called install.py to automatically install all the packages used.

I need to install multiple packages (keras, numpy, etc.).

For each package, I have to use the command os.system . I have no idea what it does, and this is the only information that I have.

Yes, this type of question was asked before, but not with several packages and this specific os.system line.

I don't know if this might work for your specific issues. Give it a go.

import os

packages = ["keras","sklearn"] #etc

for package in packages:
    os.system("pip install "+ package) #installs particular package

The way I recommend doing this is to import pip as a module, as follows: (untested)

import pip

def install(package):
    if hasattr(pip, 'main'):
        pip.main(['install', package])
    else:
        pip._internal.main(['install', package])

packages = [] #Add your packages as strings
for package in packages:
    install(package)

I used this question for most of the code.

You could create a requirements.txt file with all of your package requirements.

import os

os.system("pip install -r requirements.txt")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM