Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <a href="https://stackoverflow.com/questions/5117194/python-namespacing-and-classes/5118437#5118437"><em>Namespacing and classes</em></a>. The <code>__init__.py</code> shown there would have also allowed the <code>usepackage.py</code> script to have been written this way (<code>package</code> maps to <code>subdir</code> in your question, and <code>Class1</code> to <code>myclass01</code>, etc):</p> <pre><code>from package import * print Class1 print Class2 print Class3 </code></pre> <p><strong>Revision (updated):</strong></p> <p>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 <code>__init__.py</code> file (which also works in Python 3.4.1):</p> <pre><code>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__ </code></pre> <p>Alternatively you can put the above into a separate .py module file of its own in the package directory, and use it from the package's <code>__init__.py</code> like this:</p> <pre><code>if __name__ != '__main__': from ._import_package_files import * # defines __all__ __all__.remove('__all__') # prevent export (optional) </code></pre> <p>Whatever you name the file, it should be something that starts with an <code>_</code> underscore character so it doesn't try to <code>import</code> itself recursively.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload