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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:07:57+00:00 2026-06-14T23:07:57+00:00

I created an import hook (based on PEP 302 ), to dynamicly load python

  • 0

I created an import hook (based on PEP 302), to dynamicly load python bytecode (compiled python code) from the database.

import dyn_imports.test   #@UnresolvedImport

This works fine, but if I change the module and reload the module, the module reloads but I do not see the changes.

This is how I create the bytecode :

code_object = compile(db.Text(dyn.py_source).encode(), dyn.name.encode(), 'exec')
key_name = 'dyn_imports.' + name
dynmod = models.Dynmods(key_name = key_name, name = key_name, bytecode = db.Blob(marshal.dumps(code_object)))
dynmod.put()
reload(sys.modules[key_name])             # reload the updated code

The reload() executes the load_module part of the import hook.
This is the simplified version of the load_module function in the import hook class:

def load_module(self, mod_name):
    mod = imp.new_module(mod_name)
    mod.__loader__ = self
    dynmod = models.Dynmods.get_by_key_name(mod_name)    # load from DB
    mod.__file__ = mod_name
    exec marshal.loads(db.Blob(dynmod.bytecode)) in mod.__dict__
    sys.modules[mod_name] = mod
    return mod

When I shutdown the instance(s), it works fine.

Update : I also have tested the import loader using py source instead of bytecode. This did not work either. I have done most of the testing on the dev server.

I have created a Jinja2 module loader before, which loads Jinja compiled templates (= py source) from the datastore. When I re-create the Jinja environment and delete the modules (compiled templates) from sys.modules, the new modules are reloaded, without instance restart. So what am I doing wrong here?

  • 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-14T23:07:58+00:00Added an answer on June 14, 2026 at 11:07 pm

    I have found a solution. Besides reloading the changed module, I have to reload all modules that use (imported) this module. I have done some tests and it works fine. The correct order is also important. So now I have to figure out, how to create an import dependency graph and use it to reload modules.

    But that is a new question.

    I create a list of dependencies, when modules import the dynamic module. Using this list, I can reload the dependent modules. I also have to flag the other instances of my GAE appid. These other instances have to reload the flagged dynamic module (if used) and reload the dependent modules for the dynamic module in this other instance.

    To create unique instance ids for every instance, I added the class variable :

    instance = datetime.datetime.utcnow()
    

    to the import hook class. Memcache is used by the setter instance to flag the other instances. This flag contains per reloaded dynamic module: the flag setter instance and the reload datetime stamp of the dynamic module. Other (flag getter) instances can check, if they have to reload a dynamic module.

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

Sidebar

Related Questions

I have one SQL server database which was created using Import/Export Wizard from an
I created a java project and then tried to import Java source code from
I created a subclass from UIView #import <UIKit/UIKit.h> @interface MeeSelectDropDownView : UIView { UILabel
I've created this simple GUI: from tkinter import * root = Tk() def grabText(event):
I created the following example to understand the instances in python import time; class
The code pasted below does the following: creates an import hook creates a context
Hi I've created a C++ DLL which I import to C# application to re-use
I have created a new branch and did svn import of the source files
Hi So I have created a quick table: package models; import javax.persistence.*; import play.db.ebean.Model;
I've created a repo, and then I ran svn import . https://myrepo . It

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.