Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Imported or executed?

Reply
Thread Tools

Imported or executed?

 
 
Stephan Schulz
Guest
Posts: n/a
 
      02-16-2005
Hi All,

I've written a Python replacement of fixbb, as shell/awk script that
will create tightly fitting bounding boxes for PostScript files.

Most of the functionality is encapsulated in a small function called
fixbb(filename). I'm importing this into other code via 'import'.

A few lines of code for command line handling make the module into a
freestanding program. For simplicity and ease of maintenance, I would
rather keep library and program as a single file.

However, the command line code is also executed when the module is
imported, not only when the stand-alone program is executed. That is
not, of course, intended (or working).

Is there a (portable, standard) way for the program/module to find out
if it is imported or executed stand-alone?

You can find the code at
http://www.eprover.org/SOFTWARE/utilities.html

Bye,

Stephan

--
-------------------------- It can be done! ---------------------------------
Please email me as (Stephan Schulz)
----------------------------------------------------------------------------
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      02-16-2005
Stephan Schulz wrote:

> Is there a (portable, standard) way for the program/module to find out
> if it is imported or executed stand-alone?


if a module is executed, it's name is set to "__main__". see:

http://www.python.org/doc/faq/progra...nt-module-name

</F>



 
Reply With Quote
 
 
 
 
Jeff Shannon
Guest
Posts: n/a
 
      02-16-2005
Stephan Schulz wrote:

> Is there a (portable, standard) way for the program/module to find out
> if it is imported or executed stand-alone?


def fixbb(*filelist):
# ...

if __name__ == '__main__':
# Executed stand-alone
fixbb(sys.argv[1:])

(Obviously, you'd probably want to do more command-line checking than
this...)

Jeff Shannon
Technician/Programmer
Credit International

 
Reply With Quote
 
Stephan Schulz
Guest
Posts: n/a
 
      02-16-2005
In article <mailman.2715.1108581429.22381.python->,
Fredrik Lundh wrote:
>Stephan Schulz wrote:
>
>> Is there a (portable, standard) way for the program/module to find out
>> if it is imported or executed stand-alone?

>
>if a module is executed, it's name is set to "__main__". see:

[...]

It works. Thanks!

Bye,

Stephan

--
-------------------------- It can be done! ---------------------------------
Please email me as (Stephan Schulz)
----------------------------------------------------------------------------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why do directly imported variables behave differently than thoseattached to imported module? Dun Peal Python 10 05-03-2011 10:11 PM
"Variable ... is not imported..." using an imported variable from a module Volker Nicolai Perl Misc 9 07-04-2005 08:34 AM
Can Outlook 2000's address book be imported into Thunderbird? Galaxy Firefox 8 07-21-2004 01:38 AM
Dynamically change imported/linked stylesheet JezB ASP .Net 1 06-10-2004 02:04 PM
Firefox0.8 imported IE6 favs jumbled up tinklemagoo Firefox 2 05-24-2004 03:11 AM



Advertisments