473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Can Python Module Locate Itself?

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
6 4254
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
Try this:

print 'usage:', __file__

Kerim

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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Jeffrey Barish | last post by:
Since upgrading to python 2.4 yesterday, I am no longer able to use Tkinter. From http://www.python.org/moin/TkInter I learned that I need to edit Modules/Setup to include Tkinter during the make....
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
3
by: JimF | last post by:
I need to scan a couple of hundred databases in several directories to locate all code that uses a particular function, but cannot figure out how to read the Form or Module text itself. I am...
3
by: skip | last post by:
I'm having no success building the curses module on Solaris 8 (yes, I know it's ancient - advancing the state-of-the-art is not yet an option) for Python 2.4. Sun provides an apparently ancient...
0
by: has | last post by:
Hi all, need a little bit of advice on dynamically binding an embedded Python interpreter. First, the code for anyone that wants a look: ...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
3
by: Philip Semanchuk | last post by:
On Nov 9, 2008, at 7:00 PM, News123 wrote: Look under the heading "Standard Encodings": http://docs.python.org/library/codecs.html Note that both the page you found (which appears to be a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.