• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting module object from module name

 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Chris.. That worked
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic