Ask Your Question

Sage is not updating modules imported by an attached file

asked 2012-08-09 16:18:57 +0200

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 17:16:46 +0200 )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-11 00:30:33 +0200 )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-11 01:06:32 +0200 )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-11 02:01:03 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2021-04-01 13:59:34 +0200

Dan-K gravatar image

updated 2021-04-01 14:00:33 +0200

Not sure if this works for the sage shell, but adding the following two lines successfully reloads modules on every execution for sage notebook:

%load_ext autoreload
%autoreload 2

Source documentation:

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2012-08-09 16:18:57 +0200

Seen: 373 times

Last updated: Apr 01