BuZz
BuZz

Reputation: 17425

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 ?

Upvotes: 2

Views: 668

Answers (1)

David Webb
David Webb

Reputation: 193696

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)

Upvotes: 8

Related Questions