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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:40:28+00:00 2026-05-20T09:40:28+00:00

Here’s the structure I’m working with: directory/ script.py subdir/ __init__.py myclass01.py myclass02.py What I

  • 0

Here’s the structure I’m working with:

directory/
          script.py
          subdir/
                 __init__.py
                 myclass01.py
                 myclass02.py

What I want to do is import in script.py the classes defined in myclass01.py and myclass02.py. If I do:

from subdir.myclass01 import *

It works fine for the class defined in myclass01.py. But with this solution if there are many classes defined in different files in subdir and I want to import all of them, I’d have to type one line for each file. There must be a shortcut for this. I tried:

from subdir.* import *

But it didn’t work out.

EDIT: here are the contents of the files:

This is __init__.py (using __all__ as Apalala suggested):

__all__ = ['MyClass01','MyClass02']

This is myclass01.py:

class MyClass01:
    def printsomething():
        print 'hey'

This is myclass02.py:

class MyClass02:
    def printsomething():
        print 'sup'

This is script.py:

from subdir import *
MyClass01().printsomething()
MyClass02().printsomething()

This is the traceback that I get when I try to run script.py:

File "script.py", line 1, in <module>
    from subdir import *
AttributeError: 'module' object has no attribute 'MyClass01'
  • 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-20T09:40:29+00:00Added an answer on May 20, 2026 at 9:40 am

    Although the names used there are different from what’s shown in your question’s directory structure, you could use my answer to the question titled Namespacing and classes. The __init__.py shown there would have also allowed the usepackage.py script to have been written this way (package maps to subdir in your question, and Class1 to myclass01, etc):

    from package import *
    
    print Class1
    print Class2
    print Class3
    

    Revision (updated):

    Oops, sorry, the code in my other answer doesn’t quite do what you want — it only automatically imports the names of any package submodules. To make it also import the named attributes from each submodule requires a few more lines of code. Here’s a modified version of the package’s __init__.py file (which also works in Python 3.4.1):

    def _import_package_files():
        """ Dynamically import all the public attributes of the python modules in this
            file's directory (the package directory) and return a list of their names.
        """
        import os
        exports = []
        globals_, locals_ = globals(), locals()
        package_path = os.path.dirname(__file__)
        package_name = os.path.basename(package_path)
    
        for filename in os.listdir(package_path):
            modulename, ext = os.path.splitext(filename)
            if modulename[0] != '_' and ext in ('.py', '.pyw'):
                subpackage = '{}.{}'.format(package_name, modulename) # pkg relative
                module = __import__(subpackage, globals_, locals_, [modulename])
                modict = module.__dict__
                names = (modict['__all__'] if '__all__' in modict else
                         [name for name in modict if name[0] != '_'])  # all public
                exports.extend(names)
                globals_.update((name, modict[name]) for name in names)
    
        return exports
    
    if __name__ != '__main__':
        __all__ = ['__all__'] + _import_package_files()  # '__all__' in __all__
    

    Alternatively you can put the above into a separate .py module file of its own in the package directory—such as _import_package_files.py—and use it from the package’s __init__.py like this:

    if __name__ != '__main__':
        from ._import_package_files import *  # defines __all__
        __all__.remove('__all__')  # prevent export (optional)
    

    Whatever you name the file, it should be something that starts with an _ underscore character so it doesn’t try to import itself recursively.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here I had a problem that I am adding contact from the address book
Here is my situation. My solution structure is as follows. Project Used to handle
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see

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.