开发者

Cant get __import__() to dynamically import a module in python - I know this cause it doesn't show up in sys.modules

I wrote a small script. It's designed to search the python directory for all available modules (whether they are installed or not), then it is supposed to check what modules are currently loaded, then it offers an option to dynamically load a module of your choice. The latter using __import__() because I am passing a string to it - (this is where I am having a problem - but I'll get back to it shortly)...then it gives the option to "browse" the module for all its classes, functions, etc. (using dir([module name]) ...).

The problem:

When the module is loaded dynamically - it is embedded in a try/except statement - if it succeeds it reports that the "module is loaded" and if it fails it reports...duh..."Failed to load..."

If you type the name of a module, for example a module named "uu", it says "loaded". So I know it is loading - however, when I go back and call the function that checks all of the LOADED modules - it is blank (using sys.modules)

开发者_运维技巧

I am thinking that python is loading the module into a temporary place which is not sys.modules because when I exit out of the script and check sys.modules it is not there.


Nascent_Notes, nice script! I tried loading uu (command 3) and printing the list of loaded modules (command 2) and they both seem to work fine.

However, if I try to "browse the module" (command 4), I get the following error:

HlpWiz>>> 4
What module do you want to look more into?:  uu

*An error occurred - probably because the module isn't loaded or is misspelled*

Try running

#!/usr/bin/env python
import sys
__import__('uu')
print(sys.modules['uu'])
print(dir(uu))

You should get NameError: name 'uu' is not defined.

So it appears that although __import__ successfully imports the uu module, it does not add uu to the global namespace -- the module uu can not be accessed by the variable name uu. It can be accessed through sys.modules however:

Therefore, change

    var_mod = input("What module do you want to look more into?:  ")
    print "\n attempting to browse... please wait!"
    time.sleep(2)
    browse_mod(zlib = var_mod)

to

    var_mod = raw_input("What module do you want to look more into?:  ")
    print "\n attempting to browse... please wait!"
    time.sleep(2)
    browse_mod(zlib = sys.modules[var_mod])

Not only is using raw_input much safer than input (the user will not be able to execute unexpected/malicious commands), but also raw_input does what you want here.

On a minor note, you could also change

i = 1
for line in sample:
    print i, line
    i = i + 1

to the more pythonic

for i,line in enumerate(sample):
    print i+1, line

Edit:

sys.modules is a dict (short for dictionary). Dicts are like telephone books -- you give it a name (better known as a "key") and it returns a phone number (or more generally, a "value").

In the case of sys.modules, the keys are module names (strings). The values are the module objects themselves.

You access the values in the dict using bracket notation. So uu is just a string, but sys.modules['uu'] is the module uu.

You can read the full story on dicts here: http://docs.python.org/tutorial/datastructures.html#dictionaries

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜