By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,143 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,143 IT Pros & Developers. It's quick & easy.

Can Python Module Locate Itself?

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
sj <_r*************@swbell.net> writes:
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__


--
Juha Autero
http://www.iki.fi/jautero/
Eschew obscurity!
Jul 18 '05 #2

P: n/a
Try this:

print 'usage:', __file__

Kerim

Jul 18 '05 #3

P: n/a
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/li...-optparse.html)

HTH.
Miki
Jul 18 '05 #4

P: n/a

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
Jul 18 '05 #5

P: n/a

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

Jul 18 '05 #6

P: n/a
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.
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.