Ask Your Question
2

Sage is not updating modules imported by an attached file

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

patronics gravatar image

My file structure looks like:

project/
    __init__.py
    project.py
    bounds.py

project.py 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

bounds.py:

import abc

class BoundBase(object):
    @staticmethod
    @abc.abstractmethod
    def bound(g):
        return

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

Now, I start a Sage session in the Terminal and do attach project.py. I can even make changes in project.py and see the results immediately in Sage. The problem is that if I change something in bounds.py, 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

Comments

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 +0100 )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 +0100 )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 +0100 )edit
1

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: http://ipython.org/ipython-doc/stable/config/extensions/autoreload.html

tcoffee gravatar imagetcoffee ( 2012-08-11 02:01:03 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
0

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

Dan-K gravatar image

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

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: https://ipython.readthedocs.io/en/sta...

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

3 followers

Stats

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

Seen: 823 times

Last updated: Apr 01 '21