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

The Archive Base Latest Questions

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

In my application, I have: … folder_a/ __init__.py a.py folder_b/ __init__.py b1.py b2.py …

  • 0

In my application, I have:

...
folder_a/
    __init__.py
    a.py
    folder_b/
        __init__.py
        b1.py 
        b2.py
        ...

and I need to dynamically load modules from folder_b into a.py, based on an variable, say, module_from_b. To do this, in a.py, I do:

mymodule = __import__("folder_b.%s" % module_from_b, globals(), locals(), fromlist=["*"])

Since this is going to live inside a loop, i.e. be called numerous times, I wonder if it has any impact on the performance, or anything else? If so, is there any way to mitigate it while preserving the capability of dynamic module loading?

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

    I’ve never benchmarked it, but I believe importing (whether dynamic or static) a module that has been previously imported into the running interpreter (even by completely unrelated code) should be pretty cheap. All it really has to do should be some (successful) dictionary lookups.

    Importing a new module will of course run all the code in the module, plus the file system search to find it.

    So if you’re repeatedly importing a dynamically chosen module from a relatively small set of modules, then you shouldn’t have much of a problem so long as you can tolerate a delay the first time each specific module is used (how much of a delay depends on your modules); after a little while almost all of the modules you’re importing will already have been imported, and so the __import__ calls will become cheap.

    An alternative design you might consider: If the total set of modules you’ll need is knowable up front (either statically or dynamically), you could pre-import them before your loop to “warm up” Python’s imported module set. If there aren’t many modules in b that won’t be used you could import everything in b’s __init__.py.

    That way the import delays would be gotten out of the way at initiation time and you could use getattr on the b package to dynamically get your modules instead of needing to use __import__. Not such a good option if you’re loading so many modules that you want to spread out the import costs over your loop, or if you have many modules but only need a relatively small number of them and it’s hard to know which ones ahead of time.

    There’s another possibly even better approach, which will work if in the places where you’re identifying which module to import you’re choosing some constant string (and not, say, something read from a configuration file or user input). Rather than passing the name of the module onwards to eventually be imported elsewhere, why not import the module then and there and pass the module itself on to eventually be used elsewhere? As a made up example, since I don’t know what you’re doing, instead of:

    for module_name in ['b1', 'b2', 'b3', 'b4']:
        function_using_module(module_name)
    
    def function_using_module(module_name):
        module = __import__(...)
        ...
    

    You could do:

    from folder_b import b1, b2, b3, b4
    
    for module in [b1, b2, b3, b4]:
        function_using_module(module)
    
    def function_using_module(module):
        ...
    

    Modules are objects just like anything else, so you can store them in lists or objects or whatever you’re doing with the names. Passing the modules around directly is usually cleaner than passing the names around as proxies for the modules and later importing them.

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

Sidebar

Related Questions

My application have to load some points from internal sqlite database of android, and
I have got a requirement from a client. They need three blogging web application
My application have several modules, each in one tab on the mainform. When using
My application have 3 Relative Layouts and 3 Linear Layouts. Based on the web
We have application have been written in Silverlight using MVVM pattern. We need to
My C# application have to read some date from MySQL database. Problem I have
Users of my application have the possibility of choosing some values from list. The
If an application have to support starting from android 1.6 device and onwards. The
I have Silverlight application that retrieves data from the database through a WCF Service.
My application have performance issues, so i started to investigate this from the root:

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.