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
Register / Login


JavaRanch » Java Forums » Languages » Jython/Python
Reply 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: 276

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.


OCPJP
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1127
    
    7

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:


ex-Oracle bloke
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 276

Thank you so much Chris.. That worked
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Getting module object from module name
 
Similar Threads
Splitting a zip file and rejoining it
Mistakenly changed web.xml
How to add directory to sys.path??
Some Groovy vs Python observations.
Question about OOSL