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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:08:20+00:00 2026-06-01T20:08:20+00:00

What would be the best (read: cleanest) way to tell Python to import all

  • 0

What would be the best (read: cleanest) way to tell Python to import all modules from some folder?

I want to allow people to put their “mods” (modules) in a folder in my app which my code should check on each startup and import any module put there.

I also don’t want an extra scope added to the imported stuff (not “myfolder.mymodule.something”, but “something”)

  • 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-01T20:08:21+00:00Added an answer on June 1, 2026 at 8:08 pm

    If transforming the folder itself in a module, through the use of a __init__.py file and using from <foldername> import * suits you, you can iterate over the folder contents
    with “os.listdir” or “glob.glob”, and import each file ending in “.py” with the __import__ built-in function:

    import os
    for name in os.listdir("plugins"):
        if name.endswith(".py"):
              #strip the extension
             module = name[:-3]
             # set the module name in the current global name space:
             globals()[module] = __import__(os.path.join("plugins", name)
    

    The benefit of this approach is: it allows you to dynamically pass the module names to __import__ – while the ìmport statement needs the module names to be hardcoded, and it allows you to check other things about the files – maybe size, or if they import certain required modules, before importing them.

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

Sidebar

Related Questions

Good day all, what would be the best way to read a value from
Would it be best to read from gmail via pop3/imap? Or should/must I use
What would be the best way to read an xml file and displaying for
How best would I exclude results from #other_section, given a structure like this? <form>
In Java, what would the best way be to have a constantly listening port
Hey all, what would be best practice for clipping the bottom borders of a
I would like to set some read-only parameters (eg. an RMI registry host and
What's the best way to import a small csv file into SQL Server using
From what I have read best practice is to have classes based on an
When requesting hardware for a WebLogic server, what hardware would best improve its performance?

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.