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

22 views
Skip to first unread message

Jacob Bond

unread,
Jul 7, 2017, 1:03:15 PM7/7/17
to sage-support


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 my main block parses command line arguments and Sage aborts the load with an error because the argument parser 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 much easier to not have to do this every time I change the module.

Jacob Bond

unread,
Jul 7, 2017, 1:37:48 PM7/7/17
to sage-support
This happens both in Sage 7.3 on Ubuntu Linux (Chromebook) and in Sage 7.6 on CoCalc.
Reply all
Reply to author
Forward
0 new messages