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 8830327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:03:25+00:00 2026-06-14T08:03:25+00:00

I want to dynamically load a class from a given string. However, I do

  • 0

I want to dynamically load a class from a given string. However, I do not know which file the class will be in, so I will have to search all files. I’ve tried this, but I get AttributeError: 'module' object has no attribute 'MyClass' even though I’m 100% sure that that module (in the current iteration) has that class:

target = 'MyClass'
module_names = [mf[0:-3] for mf in os.listdir('application/models') if mf.endswith(".py")]
modules = [imp.new_module(x) for x in module_names]
for module in modules:
    try:
        target_class = getattr(module, target)
    except ImportError, AttributeError:
        continue

if target_class:
    print 'found class'

It seems I’m getting really close. What I want is not to limit the search to just one folder, but perhaps multiple folders. What’s wrong with my code?

Edit: Ok now I’m trying something like this, but still getting the same error:

    for m in module_names:
        try:
            x = reload(__import__(m))
            target_class = getattr(x, target)
        except ImportError, AttributeError:
            continue
        else:
            break

    if target_class:
        print 'found class'
  • 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-06-14T08:03:26+00:00Added an answer on June 14, 2026 at 8:03 am

    From the documentation on imp.new_module, the returned module is empty. Meaning that it will never contain your class.

    Perhaps what you want to do is add your target directory to sys.path and use __import__ to dynamically import those modules, then check for your class?


    The following code works for me:

    modules = ['foo','bar']
    for mod in modules:
        try:
            x = reload(__import__(mod))
        except ImportError:
            print "bargh! import error!"
            continue
        try:
            cls = getattr(x,'qux')
        except AttributeError:
            continue
    
    a = cls()
    print a.__class__.__name__
    

    Where foo.py and bar.py are in the same directory:

    #foo.py
    class foo(object):
        pass
    

    and:

    #bar.py
    class qux(object):
        pass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few internal Canvas xaml files which I want to load dynamically
I have a jar file, which I dynamically load during execution of my java
I'm creating a CMS, and I want to dynamically load the style from the
I have a bunch of links that I want to load dynamically within the
I have this code which reads all the files from a directory. File textFolder
We used to have a way to load properties from a file on the
I have an NSTableView that I want to load it's value from an NSArrayController.
I have a Rails Engine gem in which I want to load a HomeController
I load 2 tables (header and data) dynamically into a div. I want the
I want to dynamically create instance method of child class through class method of

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.