How to abort module evaluation?

mrstevegross mrstevegross at gmail.com
Tue May 12 14:33:02 EDT 2009


I have a python script that is pretty simple: when executed, it
imports a bunch of stuff and then runs some logic. When *imported*, it
defines some variables and exits. Here's what it looks like:

=== foo.py ===
if __name__ != '__main__':
  x = 1
  exit_somehow

import bar
do_some_stuff...
=== EOF ===

There's a problem, though. On line 3, I wrote "exit_somehow".
Actually, I have no idea how to do that part. Is there some way I can
get python to abandon processing the rest of the script at that point?
I know goto doesn't exist, so that's not an option... sys.exit() won't
work, because that will abort the entire python interpreter, rather
than just the evaluation of the module.

Thanks,
--Steve



More information about the Python-list mailing list