You can move the function in question to another file and import it into your file.
But the fact that you use everything related to import makes me think that you need to move most of the material to the imported module in functions and call them only as necessary with the help of the main defender.
def print_one(): print "one" def print_two(): print "two" def what_i_really_want_import(): print "this is what I wanted" if __name__ == '__main__': print_one() print_two()
and not what you probably have that looks like
print "one" print "two" def what_i_really_want_import(): print "this is what I wanted"
With the main guard, anything in the function will not be executed during import, although you can still call him if you need to. If the name == " main " really means that I am running this script from the command line? "When importing, the if condition returns false, so your calls to print_one (), print_two () will not be executed.
There are several good reasons to leave things in a script to execute upon import. Some of them are constants, initialization / configuration steps that you want to perform automatically. And having a module level variable is an elegant way to achieve a singleton.
def print_one(): print "one" def print_two(): print "two" time_when_loaded = time.time() class MySingleton(object): pass THE_ANSWER = 42 singleton = MySingleton()
But by and large, do not leave too much code to execute at boot, otherwise you will get exactly these problems.
Jl peyret
source share