views:

239

answers:

1

Wrote a small script - just trying to get used to the os, sys, and some other common modules/libraries available to python at install. The gist: The script is designed to search the python directory for all available modules (whether they are installed or not), then it is suppose to check what modules are currently loaded, then it offers an option to dynamically load a module of your choice - 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.

Any help would be appreciated...

http://paste.pocoo.org/show/183TNX63ezVaKIcGciiy/

for the script - it wont entirely because it requires a module/script that I borrowed from someone else. But you can see the calls I am using from the standard library in the menu options 2, 3, and 4.

Thank you,

-J

+1  A: 

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

unutbu
Thanks!!! That worked like a charm!! You are a god-send! I was trying variants of the i+1, I tried i++ and that didn't work - so I fell back on what I knew would work, didn't think to just put the expression where you suggested - thanks!Also, I knew that "input" was dangerous to use, as per pydocs, but it was the only way I could get it to not set the value assigned to the variable as a string - but you solved that by doing sys.modules[var_mod] - what do to the "[]" do? how is it passing the string? or is it just 'appending' the module to sys.modules? - confused... :PThanks btw!
Nascent_Notes
@Nascent_Notes, I've added a bit of explanation regarding sys.modules and dicts. Hope it helps.
unutbu
k duh - Im stupid lol sorry - yeah I know dicts, I didnt realize that sys.modules was a dict, so I thought that you were doing something 'special' - thanks for explaining that! Taught myself the value of type() :P. Hopefully that will help in the future...
Nascent_Notes