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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:33:07+00:00 2026-05-31T15:33:07+00:00

In a python application I’m working on I would like to dynamically load packages

  • 0

In a python application I’m working on I would like to dynamically load packages (plugins) based on information provided at runtime (e.g. from a config file).

So, I change sys.path to add paths to the plugins I want to load, this generally works fine, but not if the plugin is a namespaced package inside a namespace which has already been initialized (I assume that is the problem anyway).

Example:

# lib1 contains plugins/__init__.py and plugins/foo/__init__.py
# lib2 contains plugins/__init__.py and plugins/bar/__init__.py
# plugins is a namespace package

import sys

sys.path.append ('lib1')
import plugins.foo

sys.path.append ('lib2')
import plugins.bar

The above code fails with an ImportError, presumably because the “import plugins.foo” line initialized the plugins namespace/package and no further attempt is made to search sys.path for other packages in the namespace.

If I change the code to this:

sys.path.append ('lib1')
sys.path.append ('lib2')
import plugins.foo
import plugins.bar

Both imports work, but I’d like to add to sys.path after the plugins.foo import.

So, my questions are:

  1. Is my assumption correct that the second import fails because of the
    namespace package?
  2. Is there a work-around?
  • 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-31T15:33:08+00:00Added an answer on May 31, 2026 at 3:33 pm

    I think your diagnosis is right. When you import plugins.foo, python also loads plugins and caches it in sys.modules. I haven’t replicated your set-up, but I’d try del sys.modules['plugins'] before you import bar. If that doesn’t work, try reloading plugins:

    sys.path.append ('lib2')
    import plugins
    reload(plugins)
    import plugins.bar
    

    I must admit that reloading is, by general agreement, not a good idea in running programs. The “right” solution would be to figure out a workflow that lets you set the full sys.path before you start importing from the package. Or perhaps not to spread one package over different places.

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

Sidebar

Related Questions

There I am writing a Python application on win7. I would like to display/access
I have written a Python application and would like to give my users the
I wrote a simple Tkinter based Python application that reads text from a serial
I have a small web.py Python application that I would like to serve under
In my Python application, I would like to be able to send mail to
I am trying to create an installer from a Python application we coded. I
From within a Python application, how can I get the total amount of RAM
I have a python application like this / /crawl.py /crawl/__init__.py /crawl/john.py /tests/test_john.py What I
I am trying to run python application and execute actions based on specified interval.
I'm building a python application from some source code I've found Here I've managed

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.