Josh Smeaton Josh Smeaton - 2 months ago 10
Python Question

What is the best way to call a python script from another python script?

I have a script named which is not in a module. It just has code that should execute when the script itself is run. There are no functions, classes, methods etc. I have another script which runs as a service. I want to call from the script running as a service.


print "I am a test"
print "see! I do nothing productive."

# lots of stuff here # do whatever is in

I'm aware of one method which is opening the file, reading the contents, and basically eval'ing it. I'm assuming there's a better way of doing this. Or at least I hope so.

ars ars

The usual way to do this is something like the following.

def some_func():
    print 'in test 1, unproductive'

if __name__ == '__main__':
    # executed as script
    # do something

import test1

def service_func():
    print 'service func'

if __name__ == '__main__':
    # executed as script
    # do something