Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 510911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:09:44+00:00 2026-05-13T07:09:44+00:00

I wrote a small script. It’s designed to search the python directory for all

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:09:44+00:00Added an answer on May 13, 2026 at 7:09 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 258k
  • Answers 258k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Not sure why this is happening from the code you… May 13, 2026 at 11:01 am
  • Editorial Team
    Editorial Team added an answer Actually, if the linq query is used with a collection… May 13, 2026 at 11:01 am
  • Editorial Team
    Editorial Team added an answer I think i figured it out.. I'm using Tomalak's Byte… May 13, 2026 at 11:01 am

Related Questions

I have a problem replacing a command inside of a script, the offending line
I'm having this problem and I reached a deadlock, I would try anything I've
Right now I'm trying to serve CSS and JS files from a server that
I need the definition of XHTML tags with their properties... last time I asked
I've been parsing through some log files and I've found that some of the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.