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

  • SEARCH
  • Home
  • 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 8010657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:48:18+00:00 2026-06-04T18:48:18+00:00

Possible Duplicate: Dynamic module import in Python Probably a simple question! I need to

  • 0

Possible Duplicate:
Dynamic module import in Python

Probably a simple question! I need to iterate through a list of classes (as strings) passed from a settings file. The classes are listed like below:

TWO_FACTOR_BACKENDS = (
    'id.backends.AllowToBeDisabled', # Disable this to enforce Two Factor Authentication
    'id.backends.TOTPBackend',
    'id.backends.HOTPBackend',
    #'id.backends.YubikeyBackend',
    #'id.backends.OneTimePadBackend',
    #'id.backends.EmailBackend',
)

I now need to call the authenticate() function on each of these classes (unless commented out, of course). I’m happily iterating through the list, I just need to know how to convert the strings to a Class object in my foreach loop so that I can call the authenticate method on it. Is there an easy way to do this?

  • 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-04T18:48:20+00:00Added an answer on June 4, 2026 at 6:48 pm

    You want to use the importlib module to handle loading of modules like this, then simply use getattr() to get the classes.

    For example, say I have a module, somemodule.py which contains the class Test:

    import importlib
    
    cls = "somemodule.Test"
    module_name, class_name = cls.split(".")
    
    somemodule = importlib.import_module(module_name)
    
    print(getattr(somemodule, class_name))
    

    Gives me:

    <class 'somemodule.Test'>
    

    It’s trivial to add in things like packages:

    cls = "test.somemodule.Test"
    module_name, class_name = cls.rsplit(".", 1)
    
    somemodule = importlib.import_module(module_name)
    

    And it will not import a module/package if it’s already been imported, so you can happily do this without keeping track of loading modules:

    import importlib
    
    TWO_FACTOR_BACKENDS = (
        'id.backends.AllowToBeDisabled', # Disable this to enforce Two Factor Authentication
        'id.backends.TOTPBackend',
        'id.backends.HOTPBackend',
        #'id.backends.YubikeyBackend',
        #'id.backends.OneTimePadBackend',
        #'id.backends.EmailBackend',
    )
    
    backends = [getattr(importlib.import_module(mod), cls) for (mod, cls) in (backend.rsplit(".", 1) for backend in TWO_FACTOR_BACKENDS)]
    

     

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Dynamic module import in Python I have a list of modules, like
Possible Duplicate: How to generate a dynamic “in (…)” sql list through Spring JdbcTemplate?
Possible Duplicate: Dynamic proxy for concrete classes Once I create a InvocationHandler implementation, I
Possible Duplicate: How do you generate dynamic (parameterized) unit tests in Python? I have
Possible Duplicate: How do you generate dynamic (parameterized) unit tests in Python? Is there
Possible Duplicate: Dynamic loading of python modules python: How to add property to a
Possible Duplicate: Javascript dynamic variable name A very basic question. I want to create
Possible Duplicate: How to loop through dynamic form inputs and insert into an array
Possible Duplicate: declaring a global dynamic variable in python >>> def f(): global cat
Possible Duplicate: Dynamic LINQ OrderBy switch (sort) { case Title: queryResults = queryResults.OrderBy(r =>

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.