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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:38:57+00:00 2026-05-15T11:38:57+00:00

I have a directory structure: example.py templates/ __init__.py a.py b.py a.py and b.py have

  • 0

I have a directory structure:

example.py
templates/
    __init__.py
    a.py
    b.py

a.py and b.py have only one class, named the same as the file (because they are cheetah templates). For purely style reasons, I want to be able to import and use these classes in example.py like so:

import templates

t = templates.a()

Right now I do that by having this in the template folder’s __init__.py:

__all__ = ["a", "b"]
from . import *

However, this seems pretty poor (and maybe superfluous), and doesn’t even do what I want, as I have to use the classes like this:

t = templates.a.a()

Thoughts?

  • 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-15T11:38:58+00:00Added an answer on May 15, 2026 at 11:38 am

    To avoid repeating from <whatever> import * 25 times, you need a loop, such as:

    import sys
    
    def _allimports(modnames)
      thismod = sys.modules[__name__]
    
      for modname in modnames:
        submodname = '%s.%s' % (thismod, modname)
        __import__(submodname)
        submod = sys.modules[submodname]
        thismod.__dict__.update(submod.__dict__)
    
    _allimports('a b c d e'.split())  # or whatever
    

    I’m putting the meaningful code in a function because (a) it’s always best [[for performance and to avoid polluting the module’s namespace]], (b) in this particular case it also avoids accidents (e.g., some submodule might define a name thismod or modnames… so it’s important to keep those names that we’re using in the loop local to the function, not module globals, so they can’t be accidentally trampled;-).

    If you want to enforce the fact that a module named modname only has one class (or other global) with the same name, change the last statement of the loop to:

        setattr(thismod, modname, getattr(submod, modname))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, I have a directory structure like this: my_stuff classes one two more
I have the following directory structure: Main.py A/ __init__.py B/ __init__.py C/ __init__.py The
My directory structure is: ABC/src/com/example/model/a.java ABC/src/com/example/web/b.java Code for a.java : package com.example.model; public class
I have a directory structure like com/example/web under the root directory which contains a
I have an Active-Directory structure where User objects reside in OU for example, IT,
I have directory structure like so: |-- ROOT |-- Project1 | |-- application |
I have a directory structure that looks like this: /a/f.xml /b/f.xml /c/f.xml /d/f.xml What
I have a directory structure of directories and files that I want to render
I have a directory structure as below: / | +--- /lib | libsomething.a |
I have a directory structure as follows Client_Site/ some_folder1/ some_folder2/ some_folder3/ Lots of files

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.