Module access from inside itself

S

Steven

I'm writing a Python script which can be called from the command-line, and
I want to use my module doc string as the CL help text, but I don't know
how to access my module object from inside the module.

I've tried searching for an answer, but haven't found anything. Please
excuse me if I'm missing something simple, I'm a newbie to Python.

I'm doing something like this:



#!/usr/bin/python
"""Module doc string goes here.
"""

import getopt, sys

def MyFunction(args):
    pass

if __name__ == "__main__":
    opts, args = getop.getopt(sys.argv[1:], [], ["help"])
    for opt in opts:
        if opt == "--help":
            print MY_MODULE.__doc__    # How do I get this?
        else:
            MyFunction(args)



How do I get a reference to the module from inside the module? Is this
the Pythonic way of generating help strings for CL scripts?


Thanks,
 
O

Oren Tirosh

I'm writing a Python script which can be called from the command-line, and
I want to use my module doc string as the CL help text, but I don't know
how to access my module object from inside the module.

You don't need the module object for this. MY_MODULE.__doc__ is the
same as accessing __doc__ directly.

If you really need the current module object use sys.modules[__name__]

Oren
 
S

Scott David Daniels

Steven said:
I'm writing a Python script which can be called from the command-line, and
I want to use my module doc string as the CL help text, but I don't know
how to access my module object from inside the module.

How about this:

# demo of accessing the docstring:

"""
This is a test.

Had this been a real document, it would have said something.
"""


if __name__ == '__main__':
import __main__
print __main__.__doc__
 
B

Bengt Richter

I'm writing a Python script which can be called from the command-line, and
I want to use my module doc string as the CL help text, but I don't know
how to access my module object from inside the module.

I've tried searching for an answer, but haven't found anything. Please
excuse me if I'm missing something simple, I'm a newbie to Python.

I'm doing something like this:



#!/usr/bin/python
"""Module doc string goes here.
"""

import getopt, sys

def MyFunction(args):
    pass

if __name__ == "__main__":
    opts, args = getop.getopt(sys.argv[1:], [], ["help"])
    for opt in opts:
        if opt == "--help":
            print MY_MODULE.__doc__    # How do I get this?
            print __doc__    # should get it
        else:
            MyFunction(args)



How do I get a reference to the module from inside the module? Is this
the Pythonic way of generating help strings for CL scripts?
You are already inside the module, so an unqualified name will refer
to module globals unless it's being used in some local scope that has
a binding shadowing the global name.

Regards,
Bengt Richter
 
A

Aahz

if __name__ == "__main__":
    opts, args = getop.getopt(sys.argv[1:], [], ["help"])
    for opt in opts:
        if opt == "--help":
            print MY_MODULE.__doc__    # How do I get this?
        else:
            MyFunction(args)

import __main__
print __main__.__doc__
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top