How to abort module evaluation?

Mike Driscoll kyosohma at gmail.com
Tue May 12 15:38:34 EDT 2009


On May 12, 1:33 pm, mrstevegross <mrstevegr... at gmail.com> wrote:
> 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

Isn't abandoning the processing of the rest of the script exiting the
script? You could use "return" or wrap the offending code in a try/
except block and do something on an exception...

Mike



More information about the Python-list mailing list