sagemath/sage

init.sage file is not read when starting the notebook

Closed this issue · 12 comments

See http://ask.sagemath.org/question/3084/notebook-and-initsage. This problem seems to have been introduced in #14523.

Upstream: Completely fixed; Fix reported upstream

CC: @vbraun @ppurka @novoselt

Component: notebook

Reviewer: Karl-Dieter Crisman

Issue created by migration from https://trac.sagemath.org/ticket/15308

comment:3

An example of output caused by this bug is:

Traceback (most recent call last): #The OrthogonalCurvilinearOutput.sage file will output the coefficients and differential operators defined
File "", line 1, in

File "/tmp/tmpPnuBJt/code.py", line 7, in
attach('CartesianCoord.sage')
File "lazy_import.pyx", line 358, in sage.misc.lazy_import.LazyImport.call (sage/misc/lazy_import.c:3000)
File "/home/sagedev/sage/local/lib/python2.7/site-packages/sage/misc/attached_files.py", line 344, in attach
load(filename, globals(), attach=True)
File "/home/sagedev/sage/local/lib/python2.7/site-packages/sage/misc/preparser.py", line 1769, in load
execfile(preparse_file_named(fpath), globals)
File "/home/sagedev/.sage/temp/ubuntu/18360/CartesianCoord.sage6m7Baj.py", line 18, in
var('q_1,q_2,q_3,x_1,x_2,x_3')
NameError: name 'var' is not defined

comment:4

Afaik attach doesn't work on the notebook.

Your problem seems to be that init.sage is evaluated before Sage is started. A workaround might be to use a .py file starting with from sage.all import *

Upstream: Reported upstream. Developers acknowledge bug.

comment:6

Upstream at sagemath/sagenb#251 though I'm not sure if it's identical to the init.sage issue.

comment:7

I appear to have a fix upstream; comments welcome there. Somehow the change in #14523 in the definition of attach from

    import preparser 
        for filename in files: 
        preparser.load(filename, globals(), attach=True) 

to

    try:
        ipy = get_ipython()
    except NameError:
        ipy = None
    global attached
    for filename in files:
        if ipy:
            code = load_wrap(filename, attach=True)
            ipy.run_cell(code)
        else:
            load(filename, globals(), attach=True)

did this. Probably that should really be fixed in Sage, but I don't want to mess around with two fixes.

Can anyone think of why either adding that global (which one would think wouldn't clobber all globals, such as Integer?) or having the command-line code would be a problem? Really, neither one should in principle cause a problem. But it does.

comment:8

Indeed, a little more testing shows that globals() is definitely not the usual Sage globals at that point - it is just the globals for that module, including the things that were imported like os and the builtins. E.g.

'{\'load\': <function load at 0x1054cc488>, \'load_attach_path\': <function load_attach_path at 0x110d99e60>, \'reload_attached_files_if_modified\': <function reload_attached_files_if_modified at 0x110d9f2a8>, ...
 \'__doc__\': \'\\nKeep track of attached files\\n\\nTESTS::\\n\\n    sage: attach(\\\'http://wstein.org/loadtest.py\\\')\\n    Traceback (most recent call last):\\n    ...\\n    ...
 \'load_wrap\': <function load_wrap at 0x1054cc500>, \'load_attach_mode\': <function load_attach_mode at 0x110d99de8>, 
\'__builtins__\': {\'bytearray\': <type \'bytearray\'>, \'IndexError\': <type \'exceptions.IndexError\'>, ...}, 
...
\'os\': <module \'os\' from \'/Users/.../sage/local/lib/python/os.pyc\'>, \'modified_file_iterator\': <function modified_file_iterator at 0x110d9f230>}'

Zoinks!

comment:9

From the Python doc:

Remember that at module level, globals and locals are the same dictionary.

But on the other hand nothing really changed. Except maybe that it was in a Cython file, and now is in a Python file?

Changed upstream from Reported upstream. Developers acknowledge bug. to Completely fixed; Fix reported upstream

comment:10

Fix upstream at sagemath/sagenb#267

comment:11

This is 100% ready to go, just need a reviewer upstream!

comment:12

This was fixed and is now in a merged package.

Reviewer: Karl-Dieter Crisman