You can't, for the most part. That code isn't exposed in a useful fashion. If you are able to modify the source file, the typical solution would be to move all of that code out of the if __name__ == '__main__'
block and put it into a main()
function instead.
It's possible to use the execfile
function to sort of do what you want, as described here, but this is an ugly solution for a number of reasons (the import
statement is being used only for side effects, because you're cheating and using it to get the filename, and module level variables will probably not behave as you expect when referenced as module.var
, as described in some of the comments on that page).
And even in this example, it's not clear that there's a useful way to pass arguments. I guess you could set sys.argv
explicitly, but look how hacky this is already smelling...