Sage is not updating modules imported by an attached file

asked 2012-08-09 09:18:57 -0500

patronics gravatar image

My file structure looks like:

project/ looks approximately like:

import sys
import os

import bounds

def myfunction(g):
    if has_bound(g):

def has_bound(g):
    lbound = 1
    for name, obj in inspect.getmembers(bounds, inspect.isclass):
        if obj.__module__ == 'bounds':
            new_bound = obj.bound(g)
            if new_bound > lbound:
                lbound = new_bound
    return lbound

import abc

class BoundBase(object):
    def bound(g):

class SpecificBound(LowerBoundBase):
    def bound(g):
        return 5

Now, I start a Sage session in the Terminal and do attach I can even make changes in and see the results immediately in Sage. The problem is that if I change something in, Sage won't pick up on the changes. I've tried combinations of reset(), detach(), attach(), load(), but the only thing that works is quitting Sage completely and restarting it. There must be a better way!

edit retag flag offensive close merge delete


To avoid any comments about not being Pythonic, I'll say that since I drafted this post, I've read up on duck typing and I'm jettisoning all the abstractmethod business.

patronics gravatar imagepatronics ( 2012-08-09 10:16:46 -0500 )edit

I was just about to post the same question. It seems as though Sage internally stores a version of modules imported by attached files, which are never updated no matter what happens, though I was not able to figure out where it stores them. However, I've found that if you explicitly reload the module in the attached file, things seem to work: in your example, ... import bounds ... would be replaced with ... import bounds reload(bounds) ... You also need to re-save the attached file so that Sage will reload it.

tcoffee gravatar imagetcoffee ( 2012-08-10 17:30:33 -0500 )edit

Update: actually, even this workaround is incomplete, because any function definitions I remove in the imported module still remain in Sage, which could lead to hidden bugs.

tcoffee gravatar imagetcoffee ( 2012-08-10 18:06:32 -0500 )edit

Update: looks to me like the reloading behavior is handled by IPython: modified files are identified by `sage.misc.preparser.modified_attached_files`, which is only used by `sage.misc.interpreter`, where it calls the IPython API. In more recent versions of IPython than what I'm using, it looks like you can achieve the same behavior as the above workaround (subject to the same limitation) from within IPython using %autoreload:

tcoffee gravatar imagetcoffee ( 2012-08-10 19:01:03 -0500 )edit