Ask Your Question

Can't run Python script in Sage, error at import sage.all

asked 2014-07-17 16:53:53 +0200

Woodgnome gravatar image

updated 2014-07-17 16:56:59 +0200

I only just figured out how to run Sage in a daemon/service-like kind of state - see for the Sage script.

Now I'm trying to rewrite it to Python with Sage imports, so I can automatically start it with sage -python [PATH TO PYTHON FILE] as opposed to manually starting Sage and loading the script.

The fails with the following stack trace

Traceback (most recent call last):
  File "", line 1, in <module>
    import socket
  File "/var/www/lingapps/assets/sage/", line 4, in <module>
    import sage.all
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/", line 54, in <module>
    from sage.env import SAGE_ROOT, SAGE_DOC, SAGE_LOCAL, DOT_SAGE, SAGE_ENV
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/", line 84, in <module>
    _add_variable_or_fallback('HOSTNAME',        socket.gethostname())
AttributeError: 'module' object has no attribute 'gethostname'

This also happens when I have just a single line in the script: import sage.all.

It also fails with the following error if I just start sage -python and type import sage.all:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/", line 54, in <module>
    from sage.env import SAGE_ROOT, SAGE_DOC, SAGE_LOCAL, DOT_SAGE, SAGE_ENV
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/", line 20, in <module>
    import os, socket
  File "", line 6, in <module>
    import sage.calculus.predefined
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/calculus/", line 1, in <module>
    import all
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/calculus/", line 1, in <module>
    from calculus import maxima as maxima_calculus
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/calculus/", line 400, in <module>
    from sage.rings.all import RR, Integer, CC, QQ, RealDoubleElement, algdep
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/rings/", line 21, in <module>
    from ring import Ring
  File "ring.pyx", line 1, in init sage.rings.ring (sage/rings/ring.c:18156)
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/structure/", line 1, in <module>
    import dynamic_class # allows for sage.structure.dynamic_class?
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/structure/", line 119, in <module>
    from sage.misc.cachefunc import weak_cached_function
  File "cachefunc.pyx", line 421, in init sage.misc.cachefunc (sage/misc/cachefunc.c:17825)
  File "function_mangling.pyx", line 39, in init sage.misc.function_mangling (sage/misc/function_mangling.c:3106)
  File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/misc/", line 131, in <module>
    from sage.env import SAGE_SRC
ImportError: cannot import name SAGE_SRC

It doesn't work with sage [PATH TO SAGE FILE]either:

Traceback (most recent call last):
  File "socket ...
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-07-17 21:59:25 +0200

What is the file /var/www/lingapps/assets/sage/ doing there? My guess is that this file is messing things up: Python (via Sage) is trying to load a module socket which ought to be found in local/lib/python/, but it is loading this other file instead. Two suggestions: move that file out of the Sage directory (don't add anything to the Sage directory, in fact), and don't name your own files with the same names as standard Python modules.

edit flag offensive delete link more


In the case of `sage -python [PATH TO PYTHON FILE]` `` is the actual Python script. In the case of `sage [PATH TO SAGE FILE]` Sage compiles the `socket.sage` to a Python file called - you guess it - ``! Removing this file would be the same as removing the entire script.

Woodgnome gravatar imageWoodgnome ( 2014-07-18 09:38:37 +0200 )edit

So what happens if you rename the file to `` or `my_socket.sage` or something like that, so you don't get the conflict with Python's standard socket module?

John Palmieri gravatar imageJohn Palmieri ( 2014-07-18 16:44:31 +0200 )edit

I just realised what you meant - being stupid here. Renaming it fixed it, cheers!

Woodgnome gravatar imageWoodgnome ( 2014-07-18 16:52:28 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-07-17 16:53:53 +0200

Seen: 1,423 times

Last updated: Jul 17 '14