How to abort module evaluation?

M

mrstevegross

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
 
M

Mike Driscoll

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
 
D

Dave Angel

mrstevegross said:
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
use "else" :

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

else:
import bar
do_some_stuff...
=== EOF ===


This is rather unconventional usage, however. In fact, Mike missed the
!= in your comparison, perhaps because the other way is so common. The
usual idiom is:

import some stuff
define some globals, like x=1
def some functions, classes etc.

if __name__ == "__main__":
maybe import some extra stuff
do_some_stuff

In this case, no else is needed,

Notice also that in your case, the x=1 doesn't take effect when you use
it as a script, but only when it's being imported. Usually, this is a
mistake.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top