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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:11:14+00:00 2026-05-30T19:11:14+00:00

I have a folder structure like this: package_name/ |– module1 |– module2 |– module3

  • 0

I have a folder structure like this:

package_name/
|-- module1
|-- module2
|-- module3

What I need to do is import package_name, and then iterate over each of the modules, running code upon them one after another. The import is given at the command line, so I can’t control that ahead of time. So a user might say, run_script.py --modules='package_name.module1' or the user might simply say, run_script.py --modules='package_name.

I can do from package_name import *, and that will import everything defined in my __all__ variable in __init__.py, but from there I’m a bit stumped.

For my next step, I need a way of doing:

for module in imported:
    module.run()
  • 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-30T19:11:15+00:00Added an answer on May 30, 2026 at 7:11 pm

    I usually use a function like this to import a module by path:

    def import_by_path(path):
      if '.' not in path:
        return __import__(path, globals(), locals())
    
      path, module = path.rsplit('.', 1)
      mod = __import__(path, globals(), locals(), [module])
    
      try:
        return getattr(mod, module)
      except AttributeError:
        raise ImportError, "Could not import %s" % path
    

    Use like this:

    mod = import_by_name("itertools")
    

    Or in your case:

    for mod in map(import_by_name, imported):
      mod.run()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a folder structure like this /img/products/{product name}/ and then the sub folders
If I have a folder structure set up like this: ~/Projects emacs package1 package1-helpers
I have a folder structure like this: /some_folder /tmp /tmp/foo /tmp/foo/fu * /tmp/bar /tmp/bar/bah
I have a folder structure like this: /articles .index.php .second.php .third.php .fourth.php If I'm
If I have a folder structure that looks like this: / /bin/myComponent.cfc /reports/index.cfm How
I have a folder structure like this below: D:\myphotos\2011-02-09\1.jpg D:\myphotos\2011-02-10\2.jpg .... ...... ............ D:\myphotos\2011-02-23\10.jpg
I have a folder structure like this lots of folders another subfolder _includes getStatisticsTable.php
I have a folder structure like this and I'm trying to load the News
I have a zip file containing a folder structure like main-folder/ subFolder1/ subFolder2/ subFolder3/
I have this folder structure: package/ __init__.py misc/ __init__.py tools.py subpackage/ __init__.py submodule.py I

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.