Can Python Module Locate Itself?

S

sj

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
 
J

Juha Autero

sj said:
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 ?

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__
 
N

netytan

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.
 
M

Miki Tebeka

Hello sj,
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 ?
sys.argv[0] is always the name of the script.

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

HTH.
Miki
 
S

Skip Montanaro

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(args, "h", ["--help"])
except getopt.GetoptError, 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(sys.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
 
S

sj

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top