Sage is not updating modules imported by an attached file
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!
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.
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.
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.
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