Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Loading a module causes the __name__ == '__main__' block to execute

I have a module that can also be run from the command line:

def func()
    print('Hello!')

if __name__ == '__main__':
    func()

When I try to load it in Sage, the main block is executed:

sage: load('module.sage')
Hello!
sage:

The bigger problem is that the main block parses command line arguments and Sage aborts the load with an error because it didn't receive any command line arguments. Is there any way to handle this? I can preparse module.sage and import it as a Python module, but its so much easier to attach('module.sage') than having to reload(module) every time I change it when I'm editing.