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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:09:54+00:00 2026-06-10T01:09:54+00:00

I would like to be able to import a python module which is actually

  • 0

I would like to be able to import a python module which is actually located in a subdirectory of another module.

I am developing a framework with plug-ins.
Since I’m expecting to have a few thousands (there’s currently >250 already) and I don’t want one big directory containing >1000 files I have them ordered in directories like this, where they are grouped by the first letter of their name:

framework\
    __init__.py
    framework.py
    tools.py
    plugins\
        __init__.py
        a\
            __init__.py
            atlas.py
            ...
        b\
            __init__.py
            binary.py
            ...
        c\
            __init__.py
            cmake.py
        ...

Since I would not like to impose a burden on developers of other plugins, or people not needing as many as I have, I would like to put each plugin in the ‘framework.plugins’ namespace.
This way someone adding a bunch of private plugins can just do so by adding them in the folder framework.plugins and there provide a __init__.py file containing:

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

however, currently this setup is forcing them to also use the a-z subdirectories.
Sometimes a plugin is extending another plugin, so now I have a

from framework.plugins.a import atlas

and I would like to have

from framework.pugins import atlas

Is there any way to declare a namespace where the full name space name actually doesn’t map to a folder structure?

I am aware of the pkg_resources package, but this is only available via setuptools, and I’d rather not have an extra dependency.

import pkg_resources
pkg_resources.declare_namespace(__name__)

The solution should work in python 2.4-2.7.3

update:
Combining the provided answers I tried to get a list of all plugins imported in the __init__.py from plugins. However, this fails due to dependencies. Since a plugin in the ‘c’ folder tries to import a plugin starting with ‘t’, and this one has not been added yet.

plugins = [ x[0].find_module(x[1]).load_module(x[1]) for x in pkgutil.walk_packages([ os.path.join(framework.plugins.__path__[0], chr(y)) for y in xrange(ord('a'), ord('z') + 1) ],'framework.plugins.' ) ]

I’m not sure If I’m on the right track here, or just overcomplicating things and better write my own PEP302 importer. However, I can’t seem to find any decent examples of how these should work.

Update:
I tried to follow the suggesting of wrapping the __getattr__ function in my __init__.py, but this seems to no avail.

import pkgutil
import os
import sys
plugins = [x[1] for x in pkgutil.walk_packages([ os.path.join(__path__[0], chr(y)) for y in xrange(ord('a'), ord('z') + 1) ] )]
import types
class MyWrapper(types.ModuleType):
    def __init__(self, wrapped):
            self.wrapped = wrapped

    def __getattr__(self, name):
           if name in plugins:
                   askedattr =  name[0] + '.' + name
            else:
                    askedattr = name
            attr = getattr(self.wrapped, askedattr)
            return attr


sys.modules[__name__] = MyWrapper(sys.modules[__name__])
  • 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-06-10T01:09:56+00:00Added an answer on June 10, 2026 at 1:09 am

    Don’t use the pkgutil.extend_path function here, it tries to do the opposite of what you’re trying to accomplish:

    This will add to the package’s __path__ all subdirectories of
    directories on sys.path named after the package.
    This is useful if one wants to distribute different parts of a
    single logical package as multiple directories.

    Just extending __path__ with the subdirectories in your framework.plugins.__init__.py works just fine.

    So the solution to this problem is: put this in your __init__.py:

    __path__.extend([os.path.join(__path__[0],chr(y)) for y in range(ord('a'),ord('z')+1)])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a python script which I would like to be able to both
I would like to be able to import TFS tasks / work items into
The following: >>>import struct >>>struct.unpack('!d', ('40d11998e5035288').decode('hex'))[0] >>> >>>17510.3889778429 I would like to be able
I would like to be able to define a function which accepts an argument
I would like to be able to connect to Oracle 10.1.0.2.0 (which is installed
I am using python 2.7.1 I would like to be able to restart the
I have a long-running Python server and would like to be able to upgrade
I would like to be able to dynamically retrieve the current executing module or
Would like to be able to set colors of headings and such, different font
I would like to be able to output the name of a variable, along

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.