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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:22:05+00:00 2026-05-25T01:22:05+00:00

I have a project in pure Python with a rudimentary plugin system: you write

  • 0

I have a project in pure Python with a rudimentary plugin system: you write a module that defines a class with a specific interface and name, and the program imports the module and subsequently instantiates the class as needed.

Currently, the plugins all come from a specific folder (subdirectory of where the main .py file is located). I would like to be able to have them elsewhere on disk, and instruct the program to look for plugins in a specific place. Can I do this, for one-off dynamic imports, in a cleaner way than modifying sys.path? I don’t want to pollute this global.

Related: can I count on sys.path[0] being the path to the script, even if that differs from the current working directory (os.getcwd())?

EDIT: I forgot to mention – I want to be able to get plugins from several different folders, with the user specifying paths to plugin folders. Currently, each of these folders is set up as a package (with an __init__.py); I can trivially scrap this if it causes a problem.

  • 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-25T01:22:06+00:00Added an answer on May 25, 2026 at 1:22 am

    This might seem weird, but you can modify a module’s __path__ variable and then import from it. Then you’re not messing with the global import space in sys.path.

    Edit: If the directories are loaded at run time, then you don’t need a plugins.py file to store them. You can create the module dynamically:

    main.py:

    #create the plugins module (pseudo-package)
    
    import sys, os
    
    sys.modules['plugins'] = plugins = type(sys)('plugins')
    
    plugins.__path__ = []
    for plugin_dir in ['plugins1', 'plugins2']:
        path = os.path.join(sys.path[0], 'addons', plugin_dir)
        plugins.__path__.append(path)
    

    After creating the dynamic module, you can load the plugins as before, using either import_module or __import__:

    from importlib import import_module
    
    myplugins = []
    for plugin in ['myplugin1', 'myplugin2']:
        myplugins.append(import_module('plugins.' + plugin))
        myplugins[-1].init()
    
    ##or using __import__:
    
    myplugins = []
    for plugin in ['myplugin1', 'myplugin2']:
        myplugins.append(getattr(__import__('plugins.' + plugin), plugin))
        myplugins[-1].init()
    

    addons/plugins1/myplugin1.py:

    def init():
        print('myplugin1')
    

    addons/plugins2/myplugin2.py:

    def init():
        print('myplugin2')
    

    I’ve never used this, but it does work in both Python 2 & 3.

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

Sidebar

Related Questions

I have inherited a pure C project that uses GNU Pth ( http://www.gnu.org/software/pth/ )
I am extending an existing C++ project. I have a base class that derives
I have project that I'm working on that is going to require a webserver.
I have a project that I'm currently working on but it currently only supports
I have a project that I would like to start beta testing soon, it
I have a project written in Java (>1.5). Is it possible to write parts
I found a simple pure python blowfish implementation that meets my needs for a
We have a small project that involves automatically parsing some server/mail logs (among other
I have a project where I would like to generate a report export in
I have C++ project (VS2005) which includes header file with version number in #define

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.