PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 14, 2022

[FIXED] How to dynamically import modules with exec

 September 14, 2022     exec, python     No comments   

Issue

Now I want to built a function get_doc( ) which can get the doc of the module Here's the code

def get_doc(module):
    exec "import module"
    print module.__doc__

And the information returned:

Traceback (most recent call last):
  File "<pyshell#36>", line 1, in <module>
    get_doc(sys)
NameError: name 'sys' is not defined

Solution

The problem is you're importing "module" instead of the specified module, and you didn't put the name module anywhere. A stupid fix to this would be to always using exec

def get_doc(module):
    exec "import {}".format(module)
    exec "print {}.__doc__".format(module)"

But instead of exec, i would advise you to use the __import__ function:

def get_doc(module):
    module = __import__(module)
    print module.__doc__

Which allows more flexibility, and you can modify, use module as you wanted.



Answered By - aIKid
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing