Complete importing the module tirelessly?

I would like to use if __name__ != '__main__': and then complete the script at that moment when it will be imported, instead of the usual if __name__ == '__main__': and indent all the rest of the code in the file.

However, I could not determine what could cause only this, and not have other unwanted side effects. sys.exit() stops the entire interpreter and all other things that I tried, either throw some kind of exception, or are illegal.

Update:

I chose @trutheality's answer because it does what I want and it is very easy to start using. However, I thought some of the other answers were very interesting and / or smart - thanks to everyone who responded and plan to investigate some of them as much as possible. I had no idea that what I want could be so involved.

+4
source share
4 answers

Another hack:

 # code if __name__ == "__main__": exec(""" # main code #""") 

So ... you have lost the indentation, but also the syntax highlighting and any other editor functions that you used, unless you comment on the if line with each edit.

+2
source

Very similar to my original answer, but a little worse

partialimport.py

 class PartialImport(Exception): def __init__(self, locals, msg = ""): self.module = locals 

main.py

 from partialimport import PartialImport try: import foo except PartialImport, e: #Note e.module and therefore foo will be a Dict and not a module instance! foo = e.module 

foo.py

 from partialimport import PartialImport class Boo: pass if __name__ != "__main__": raise PartialImport(locals()) class Foo: pass 

Renouncement

This is a fantastically terrible hack that will increase the likelihood of your peers killing you, but it really works.

+2
source

main.py

 try: import foo except: print "Failed to import foo" 

foo.py

At the top of the file

 if __name__ != "__main__": raise RunTimeError("foo must be run as main, not as a module.") class foo(Object): pass 

Since python processes the file sequentially, then the Foo class will not be defined.

Another thought would be to overload the import logic through PEP 302

+1
source

Best IMO solution: there are two files.

module_main.py

 import actual_module.py if __name__ != '__main__': raise RunTimeError("You should be importing actual_module.py, not module_main.py") # Your "main" code 

actual_module.py

 # Actual module code (classes, functions, etc) 

This is "pure" in the sense that an exception is thrown only when something is really wrong - no one should import module_main.py , they should import actual_module.py .

April Fools Solution

If you are using python 2.3, there is a goto module from entrian that seems to work! It was made as a joke in April, and should never be used (if you look at the source, you will understand why: this adds a lot of overhead), but, as a proof of concept, it seems like the only way I can find to perform what you want in some kind of compressed form.

 from goto import goto, label # Code that should always be imported (classes etc.) if __name__ != "__main__": goto .end # Stuff to be executed when this is main, NOT indented label .end 
+1
source

All Articles