aspose file tools*
The moose likes Jython/Python and the fly likes Getting module object from module name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Jython/Python
Bookmark "Getting module object from module name" Watch "Getting module object from module name" New topic
Author

Getting module object from module name

R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

I have a requirement where, I have to iterate over a directory, and for every python file in that directory, I have to print its documentation..
Now, one problem I am facing in this is: - "To get the module object from the module name, so that I can USE __doc__ attribute printed"
The code I have written so far is: -

This is my main chunk of code..
Every time I print the "doc", the documentation of string module gets printed..
What I can I change in this??
As in the above code: - I have tried to use (sys.modules[modulename] ) also..
But, it is working for some module, and not for other...

E.g:- sys.modules['abc'] is working fine., but sys.modules['aifc'] is not working..
I'm trying to print doc for all the python files under Python Standard Lib directory.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1873
    
  16

As you know, I'm new to Python, but maybe the reason you keep getting the doc-string for the string class is because you are calling __doc__ on a string (the name)?

This blog post might be relevant for you: http://www.blog.pythonlibrary.org/2012/07/31/advanced-python-how-to-dynamically-load-modules-or-classes/

Not sure if I understand what you're doing, but you can use __import__ and getattr to get a handle on a module or class so you can call __doc__ on the module/class e.g. to get the doc-string for the os module and the os.path class (on Python 3):



This produces the following output:


No more Blub for me, thank you, Vicar.
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Thank you so much Chris.. That worked
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting module object from module name