How to tell if I'm being run from a shell or a module

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dg.google.groups@thesamovar.net

    How to tell if I'm being run from a shell or a module

    Hi all,

    Is there any standard way to tell if the user is running from a module
    or from an interactive shell like IDLE or IPython? The best I've come
    up with so far is for a function to look at
    getouterframes( currentframe())[1][1] (the filename in the frame record
    of the frame that called the function), and check if it exists or not
    with os.path.exists. IPython gives '(ipython console)' and IDLE gives
    'pyshell#0' whereas running from a module gives its filename. This
    seems a bit hacky. Any better ideas?

    --
    Dan Goodman

  • Jeff

    #2
    Re: How to tell if I'm being run from a shell or a module

    Conventionally, you use:

    if __name__ == '__main__':
    # do something as a script

    Comment

    • Chris

      #3
      Re: How to tell if I'm being run from a shell or a module

      On Feb 14, 7:21 pm, dg.google.gro.. .@thesamovar.ne t wrote:
      Hi all,
      >
      Is there any standard way to tell if the user is running from a module
      or from an interactive shell like IDLE or IPython? The best I've come
      up with so far is for a function to look at
      getouterframes( currentframe())[1][1] (the filename in the frame record
      of the frame that called the function), and check if it exists or not
      with os.path.exists. IPython gives '(ipython console)' and IDLE gives
      'pyshell#0' whereas running from a module gives its filename. This
      seems a bit hacky. Any better ideas?
      >
      --
      Dan Goodmanhttp://thesamovar.net/contact
      If you're just trying to prevent some actions from happening if
      something loads your script as a module just put the 'action items'
      under an if like:
      if __name__ == '__main__':
      do_the_cool_stu ff()

      All your functions inside the file will remain in-tact but it won't
      execute anything.

      Comment

      • dg.google.groups@thesamovar.net

        #4
        Re: How to tell if I'm being run from a shell or a module

        Thanks for the replies, but it's not what I meant. What I want to be
        able to determine is whether or not the user is running from an
        interactive shell (like IPython or IDLE). Checking if
        __name__=='__ma in__' checks if the current module is the one being
        run, but suppose you have two modules A and B, with the function f
        defined in module B that should print 'Interactive' or 'Module' say.
        The module A just consists of: import B; B.f(). Now whenever f is
        called, __name__ will not be '__main__' for it. Someone using IDLE
        could write import B then B.f() too. The question is: is there a way
        for f to determine if someone was using an interactive shell to call
        it or if it was being called some other way. The way I came up with
        works in these limited cases but won't work in a more general
        situation (but perhaps there is no way for it to know in the more
        general situation).

        Dan

        On Feb 14, 7:01 pm, Chris <cwi...@gmail.c omwrote:
        If you're just trying to prevent some actions from happening if
        something loads your script as a module just put the 'action items'
        under an if like:
        if __name__ == '__main__':
        do_the_cool_stu ff()
        >
        All your functions inside the file will remain in-tact but it won't
        execute anything.

        Comment

        • Gabriel Genellina

          #5
          Re: How to tell if I'm being run from a shell or a module

          En Thu, 14 Feb 2008 19:09:10 -0200, <dg.google.grou ps@thesamovar.n et>
          escribió:
          Thanks for the replies, but it's not what I meant. What I want to be
          able to determine is whether or not the user is running from an
          interactive shell (like IPython or IDLE). Checking if
          __name__=='__ma in__' checks if the current module is the one being
          run, but suppose you have two modules A and B, with the function f
          defined in module B that should print 'Interactive' or 'Module' say.
          The module A just consists of: import B; B.f(). Now whenever f is
          called, __name__ will not be '__main__' for it. Someone using IDLE
          could write import B then B.f() too. The question is: is there a way
          for f to determine if someone was using an interactive shell to call
          it or if it was being called some other way. The way I came up with
          works in these limited cases but won't work in a more general
          situation (but perhaps there is no way for it to know in the more
          general situation).
          It depends on what you mean by "an interactive shell"? If you start your
          script with:
          python -i whatever.py
          is it an interactive shell or not?

          I tried these two criteria:
          a) See if the __main__ module has a __file__ attribute.
          b) See if sys.stdin is a real tty

          These are the results I got on Windows for several configurations:

          <test.py>
          import sys
          print "__main__ has __file__", hasattr(sys.mod ules['__main__'], '__file__')
          import os
          print "sys.stdin is a tty", hasattr(sys.std in, "fileno") and
          os.isatty(sys.s tdin.fileno())
          </test.py>

          python test.py
          __main__ has __file__ True
          sys.stdin is a tty True

          python -i test.py
          __main__ has __file__ True
          sys.stdin is a tty True

          python test.py <nul >nul
          __main__ has __file__ True
          sys.stdin is a tty True

          python test.py <emptyfile >nul
          __main__ has __file__ True
          sys.stdin is a tty False

          pythonw.exe (the consoleless Python executable for Windows)
          __main__ has __file__ True
          sys.stdin is a tty False

          IDLE
          __main__ has __file__ False
          sys.stdin is a tty False

          pythonwin.exe
          __main__ has __file__ False
          sys.stdin is a tty False


          --
          Gabriel Genellina

          Comment

          • Sion Arrowsmith

            #6
            Re: How to tell if I'm being run from a shell or a module

            Gabriel Genellina <gagsl-py2@yahoo.com.a rwrote:
            >a) See if the __main__ module has a __file__ attribute.
            >b) See if sys.stdin is a real tty
            c) See if sys.argv[0] != ''

            (Although this works for the command line interactive shell, I've a
            suspicion it will fail with IDLE. But I don't have IDLE to hand to
            check.)

            --
            \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
            "Frankly I have no feelings towards penguins one way or the other"
            -- Arthur C. Clarke
            her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

            Comment

            Working...