Imported or executed?

Discussion in 'Python' started by Stephan Schulz, Feb 16, 2005.

  1. 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 -muenchen.de (Stephan Schulz)
    ----------------------------------------------------------------------------
    Stephan Schulz, Feb 16, 2005
    #1
    1. Advertising

  2. Fredrik Lundh, Feb 16, 2005
    #2
    1. Advertising

  3. Stephan Schulz

    Jeff Shannon Guest

    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
    Jeff Shannon, Feb 16, 2005
    #3
  4. In article <>,
    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 -muenchen.de (Stephan Schulz)
    ----------------------------------------------------------------------------
    Stephan Schulz, Feb 16, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JezB
    Replies:
    1
    Views:
    1,860
  2. Jon Paal
    Replies:
    0
    Views:
    346
    Jon Paal
    Jul 30, 2005
  3. Michael Abbott

    Module executed twice when imported!

    Michael Abbott, Jun 28, 2006, in forum: Python
    Replies:
    10
    Views:
    469
    Georg Brandl
    Jul 1, 2006
  4. Dun Peal
    Replies:
    10
    Views:
    443
    Chris Rebert
    May 3, 2011
  5. Volker Nicolai
    Replies:
    9
    Views:
    909
    Fabian Pilkowski
    Jul 4, 2005
Loading...

Share This Page