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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:53:55+00:00 2026-05-11T01:53:55+00:00

I have a bunch of Python modules in a directory, all being a derivate

  • 0

I have a bunch of Python modules in a directory, all being a derivate class. I need a ‘runner’ script that, for each module, instantiate the class that is inside it (the actual class name can be built by the module file name) and than call the ‘go’ method on each of them.

I don’t know how many modules are there, but I can list all of them globbing the directory via something like ‘bot_*.py’

I think this is something about ‘meta programming’, but how could be the best (most elegant) way to do it?

  • 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. 2026-05-11T01:53:56+00:00Added an answer on May 11, 2026 at 1:53 am
    def run_all(path):     import glob, os     print 'Exploring %s' % path     for filename in glob.glob(path + '/*.py'):         # modulename = 'bot_paperino'         modulename = os.path.splitext(os.path.split(filename)[-1])[0]         # classname = 'Paperino'         classname = modulename.split('bot_')[-1].capitalize()         # package = 'path.bot_paperino'         package = filename.replace('\\', '/').replace('/', '.')[:-3]         mod = __import__(package)         if classname in mod.__dict__[modulename].__dict__.keys():             obj = mod.__dict__[modulename].__dict__[classname]()             if hasattr(obj, 'go'):                 obj.go()  if __name__ == '__main__':     import sys     # Run on each directory passed on command line     for path in sys.argv[1:]:         run_all(sys.argv[1]) 

    You need a __init__.py in each path you want to ‘run’. Change ‘bot_’ at your will. Run on windows and linux.

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

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Take a look at GCov: http://gcc.gnu.org/onlinedocs/gcc/Gcov.html May 11, 2026 at 3:32 pm
  • added an answer Try doing this instead: $guests = array(); $sql = "SELECT… May 11, 2026 at 3:32 pm
  • added an answer The way it's done in django.views.generic.date_based is: {'date_field__range': (datetime.datetime.combine(date, datetime.time.min),… May 11, 2026 at 3:32 pm

Related Questions

I'm working on a Python package named lehmer that includes a bunch of extension
I've written a setup.py script for py2exe, generated an executable for my python GUI
Fedora Core 9 seems to have FCGID instead of FastCGI as a pre-built, YUM-managed
I have a bunch of C files that are generated by a collection of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.