Ask Your Question

Revision history [back]

Just to make @niles' answer more explicit: You can add the following function to the beginning of your main sage script.

## Hack to import my own sage scripts
def my_import(module_name, func_name='*'):
    import os
    os.system('sage --preparse ' + module_name + '.sage')
    os.system('mv ' + module_name + '.sage.py ' + module_name + '.py')
    #sage_eval('None', cmds='from ' + module_name + ' import ' + func_name)

    from sage.misc.python import Python
    python = Python()
    python.eval('from ' + module_name + ' import ' + func_name, globals())

Then, to import all functions from my_lib.sage, you can do

my_import("my_lib")  # from my_lib import *

To import a particular function, say my_func, you can do

my_import("my_lib", "my_func") # from my_lib import my_func

(I created this function using this solution)

Just to make @niles' answer more explicit: You can add the following function to the beginning of your main sage script.

## Hack to import my own sage scripts
def my_import(module_name, func_name='*'):
    import os
    os.system('sage --preparse ' + module_name + '.sage')
    os.system('mv ' + module_name + '.sage.py ' + module_name + '.py')
    #sage_eval('None', cmds='from ' + module_name + ' import ' + func_name)

    from sage.misc.python import Python
    python = Python()
    python.eval('from ' + module_name + ' import ' + func_name, globals())

Then, to import all functions from my_lib.sage, you can do

my_import("my_lib")  # from my_lib import *

To import a particular function, say my_func, you can do

my_import("my_lib", "my_func") # from my_lib import my_func

(I created this function using this solution)