简体   繁体   中英

Call a python main from another python script

I have been giving some huge command line tool from a colleague. The main reads a bunch of arguments, parses those using the elegant import OptionParser later on and does the job.

if __name__ == '__main__':
    main(sys.argv)

I can either dig into the code and copy paste loads of code, or find a way to use a "command line" call from my python script. I guess the second option is preferrable as it prevents me from randomly extracting code. Would you agree ?

You don't need to do cut and paste or launch a new Python interpreter. You should be able to import the other script .

For example, if your colleague's script is called somescript.py you could do:

import somescript
args = ['one','two']
somescript.main(args)

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