Can Python Module Locate Itself?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sj

    Can Python Module Locate Itself?

    I have written several small shell utilities in Python and typically use
    comments at the start of the source file as documentation. A command line
    option allows the user to read this documentation. The problem is that I
    have to explicitly code the source files location within the source which
    is not very portable. Is there anyway for a python module to locate its
    own source ?

    Thanks
  • Juha Autero

    #2
    Re: Can Python Module Locate Itself?

    sj <_remove_jones5 7@swbell.net> writes:
    [color=blue]
    > I have written several small shell utilities in Python and typically use
    > comments at the start of the source file as documentation. A command line
    > option allows the user to read this documentation. The problem is that I
    > have to explicitly code the source files location within the source which
    > is not very portable. Is there anyway for a python module to locate its
    > own source ?[/color]

    I don't know about locating source, but does the documentation have to
    be in comments? I think documentation strings exist for this purpose.

    example.py:
    """
    This is an example file. By using command line argument '-h' user can
    print this documentation.
    """
    import sys
    if sys.argv[1] == "-h":
    print __doc__




    --
    Juha Autero

    Eschew obscurity!


    Comment

    • Kerim

      #3
      Re: Can Python Module Locate Itself?

      Try this:

      print 'usage:', __file__

      Kerim



      Comment

      • Miki Tebeka

        #4
        Re: Can Python Module Locate Itself?

        Hello sj,
        [color=blue]
        > I have written several small shell utilities in Python and typically use
        > comments at the start of the source file as documentation. A command line
        > option allows the user to read this documentation. The problem is that I
        > have to explicitly code the source files location within the source which
        > is not very portable. Is there anyway for a python module to locate its
        > own source ?[/color]
        sys.argv[0] is always the name of the script.

        Have you looked at the new optparse module?
        (http://www.python.org/doc/current/li...-optparse.html)

        HTH.
        Miki

        Comment

        • netytan

          #5
          Re: Can Python Module Locate Itself?


          You can use sys.argv[0] to get the path of the program and use that to
          read in the program file.. you could then parse the source as usual.



          But personally i'd be enclined to go with Juha's idea, it seems more
          elegant and requires less work on the part of the program and the
          programmer..



          Have fun,

          Mark.


          --
          Posted via http://dbforums.com

          Comment

          • Skip Montanaro

            #6
            Re: Can Python Module Locate Itself?


            sj> I have written several small shell utilities in Python and typically
            sj> use comments at the start of the source file as documentation. A
            sj> command line option allows the user to read this documentation. The
            sj> problem is that I have to explicitly code the source files location
            sj> within the source which is not very portable. Is there anyway for a
            sj> python module to locate its own source ?

            This doesn't answer your question about locating the source file (yes, you
            can most of the time, check the __file__ global), but I start out with this
            template when creating a new script:

            #!/usr/bin/env python

            """
            Python Script Template

            usage %(prog)s [ -h ] ...

            -h - print this documentation and exit.
            """

            import sys
            import getopt

            prog = sys.argv[0]

            def usage(msg=None) :
            if msg is not None:
            print >> sys.stderr, msg
            print >> sys.stderr, __doc__.strip() % globals()

            def main(args):
            try:
            opts, args = getopt.getopt(a rgs, "h", ["--help"])
            except getopt.GetoptEr ror, msg:
            usage(msg)
            return 1

            for opt, arg in opts:
            if opt in ("-h", "--help"):
            usage()
            return 0

            return 0

            if __name__ == "__main__":
            sys.exit(main(s ys.argv[1:]))

            This guarantees I always have a help capability, no matter how trivial.
            (This is not original with me. I gleaned all the idioms used in this script
            from this group over the years.)

            Skip

            Comment

            • sj

              #7
              Re: Can Python Module Locate Itself?

              Thanks all for the many useful suggestions.

              One of the reasons I placed the docs in comments is that I have a modified
              version of head which outputs the initial comment block of a file. Its
              smart enough to recognize c++, java, python and lisp commenting styles. On
              reflection I'm not sure this approach offers any advantage over the elegant
              and standard python doc string. Also it would be easy to to update the
              modified head program to look for any initial doc strings.

              Comment

              Working...