Can't import sage into Python

859 views
Skip to first unread message

Jole Bradbury

unread,
Jul 8, 2014, 2:53:33 PM7/8/14
to sage-s...@googlegroups.com
I have a Django project with
views.py:

#!/usr/bin/env sage -python

from django.shortcuts import render
from django.http import HttpResponse
import sys
from django.http import HttpRequest
from django.template import RequestContext, loaders
sys
.path.append('/Users/Jole/Desktop/django_proj/mysite/sage/src/bin')
sys
.path.append('/Users/Jole/Desktop/django_proj/mysite/sage/')
from sage.all import *


def index(request):
   
    querystring
= request.GET.get('querystring')
   
   
return HttpResponse(querystring)


As you can see I am trying to import sage as a library into my Python document so I can do some sage computations.

Unfortunately, when I fire up my Django server on localhost, I get: No module named sage.all

As an error message. As you can see I have already tried appending to the path. My Python Path includes sage and I can see this on my Django page, however my "PATH" is
PATH
'/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin'


According to the Django server output. I'm not sure if this could be the problem. Any help would be greatly appreciated.

Jole Bradbury

unread,
Jul 8, 2014, 10:00:13 PM7/8/14
to sage-s...@googlegroups.com
Anyone??

kcrisman

unread,
Jul 8, 2014, 10:40:26 PM7/8/14
to sage-s...@googlegroups.com
Anyone??


As you know, I've already had several useless ideas for you on another forum :( but here is another one.  Is Sage itself in your regular path?  By that I mean that in my PATH I have

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/texbin:/usr/texbin:/Applications/MathApps:

where the last one is where I have a symbolic link to my Sage installation.

lrwxr-xr-x   1 me  admin         87 Oct 10  2013 sage -> /Users/.../Sage-5.12-OSX-64bit-10.6.app/Contents/Resources/sage/sage

I was able to successfully do what you wanted, though, with a file

#!/usr/bin/env sage -python

from sage.all import *
print permutations(5)


Now, I just executed that file as a Unix file.  When you are executing yours as a *Python* file, I get the same error as you.

$ python views.py 
Traceback (most recent call last):
  File "views.py", line 3, in <module>
    from sage.all import *
ImportError: No module named sage.all

Solution?  I suppose either find a way to have Django execute it as a Unix file (not Python), or just run your entire project with "sage -python" instead of your system Python!

Other people who actually know what they are talking about: please respond!

kcrisman

unread,
Jul 8, 2014, 10:41:34 PM7/8/14
to sage-s...@googlegroups.com


On Tuesday, July 8, 2014 10:40:26 PM UTC-4, kcrisman wrote:
Anyone??


As you know, I've already had several useless ideas for you on another forum :( but here is another one.  Is Sage itself in your regular path?  By that I mean that in my PATH I have

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/texbin:/usr/texbin:/Applications/MathApps:

where the last one is where I have a symbolic link to my Sage installation.

lrwxr-xr-x   1 me  admin         87 Oct 10  2013 sage -> /Users/.../Sage-5.12-OSX-64bit-10.6.app/Contents/Resources/sage/sage

I was able to successfully do what you wanted, though, with a file

#!/usr/bin/env sage -python

from sage.all import *
print permutations(5)


Now, I just executed that file as a Unix file.  When you are executing yours as a *Python* file, I get the same error as you.

$ python views.py 
Traceback (most recent call last):
  File "views.py", line 3, in <module>
    from sage.all import *
ImportError: No module named sage.all

Solution?  I suppose either find a way to have Django execute it as a Unix file (not Python), or just run your entire project with "sage -python" instead of your system Python!


And indeed,

$ sage -python views.py 
views.py:4: DeprecationWarning: Use the Permutations object instead.
  print permutations(5)
[[1, 2, 3, 4, 5], [1, 2, 3, 5, 4], [1, 2, 4, 3, 5], [1, 2, 4, 5, 3], [1, 2, 5, 3, 4], [1, 2, 5, 4, 3], [1, 3, 2, 4, 5], [1, 3, 2, 5, 4], [1, 3, 4, 2, 5], [1, 3, 4, 5, 2], [1, 3, 5, 2, 4], [1, 3, 5, 4, 2], [1, 4, 2, 3, 5], [1, 4, 2, 5, 3], [1, 4, 3, 2, 5], [1, 4, 3, 5, 2], [1, 4, 5, 2, 3], [1, 4, 5, 3, 2], [1, 5, 2, 3, 4], [1, 5, 2, 4, 3], [1, 5, 3, 2, 4], [1, 5, 3, 4, 2], [1, 5, 4, 2, 3], [1, 5, 4, 3, 2], [2, 1, 3, 4, 5], [2, 1, 3, 5, 4], [2, 1, 4, 3, 5], [2, 1, 4, 5, 3], [2, 1, 5, 3, 4], [2, 1, 5, 4, 3], [2, 3, 1, 4, 5], [2, 3, 1, 5, 4], [2, 3, 4, 1, 5], [2, 3, 4, 5, 1], [2, 3, 5, 1, 4], [2, 3, 5, 4, 1], [2, 4, 1, 3, 5], [2, 4, 1, 5, 3], [2, 4, 3, 1, 5], [2, 4, 3, 5, 1], [2, 4, 5, 1, 3], [2, 4, 5, 3, 1], [2, 5, 1, 3, 4], [2, 5, 1, 4, 3], [2, 5, 3, 1, 4], [2, 5, 3, 4, 1], [2, 5, 4, 1, 3], [2, 5, 4, 3, 1], [3, 1, 2, 4, 5], [3, 1, 2, 5, 4], [3, 1, 4, 2, 5], [3, 1, 4, 5, 2], [3, 1, 5, 2, 4], [3, 1, 5, 4, 2], [3, 2, 1, 4, 5], [3, 2, 1, 5, 4], [3, 2, 4, 1, 5], [3, 2, 4, 5, 1], [3, 2, 5, 1, 4], [3, 2, 5, 4, 1], [3, 4, 1, 2, 5], [3, 4, 1, 5, 2], [3, 4, 2, 1, 5], [3, 4, 2, 5, 1], [3, 4, 5, 1, 2], [3, 4, 5, 2, 1], [3, 5, 1, 2, 4], [3, 5, 1, 4, 2], [3, 5, 2, 1, 4], [3, 5, 2, 4, 1], [3, 5, 4, 1, 2], [3, 5, 4, 2, 1], [4, 1, 2, 3, 5], [4, 1, 2, 5, 3], [4, 1, 3, 2, 5], [4, 1, 3, 5, 2], [4, 1, 5, 2, 3], [4, 1, 5, 3, 2], [4, 2, 1, 3, 5], [4, 2, 1, 5, 3], [4, 2, 3, 1, 5], [4, 2, 3, 5, 1], [4, 2, 5, 1, 3], [4, 2, 5, 3, 1], [4, 3, 1, 2, 5], [4, 3, 1, 5, 2], [4, 3, 2, 1, 5], [4, 3, 2, 5, 1], [4, 3, 5, 1, 2], [4, 3, 5, 2, 1], [4, 5, 1, 2, 3], [4, 5, 1, 3, 2], [4, 5, 2, 1, 3], [4, 5, 2, 3, 1], [4, 5, 3, 1, 2], [4, 5, 3, 2, 1], [5, 1, 2, 3, 4], [5, 1, 2, 4, 3], [5, 1, 3, 2, 4], [5, 1, 3, 4, 2], [5, 1, 4, 2, 3], [5, 1, 4, 3, 2], [5, 2, 1, 3, 4], [5, 2, 1, 4, 3], [5, 2, 3, 1, 4], [5, 2, 3, 4, 1], [5, 2, 4, 1, 3], [5, 2, 4, 3, 1], [5, 3, 1, 2, 4], [5, 3, 1, 4, 2], [5, 3, 2, 1, 4], [5, 3, 2, 4, 1], [5, 3, 4, 1, 2], [5, 3, 4, 2, 1], [5, 4, 1, 2, 3], [5, 4, 1, 3, 2], [5, 4, 2, 1, 3], [5, 4, 2, 3, 1], [5, 4, 3, 1, 2], [5, 4, 3, 2, 1]]

Guess I need to get used to using Permutations instead of permutations.

- kcrisman 

kcrisman

unread,
Jul 9, 2014, 9:05:19 AM7/9/14
to sage-s...@googlegroups.com
If anyone else who knows more about this stuff is following the conversation, I'd appreciate an eye thrown on my updated answer at http://stackoverflow.com/questions/24636822/how-to-properly-import-sage/ which involves writing a Python script whose sole purpose is to call a shell script calling the Python in Sage ... well, the customer is always right :)

Nils Bruin

unread,
Jul 9, 2014, 11:18:35 AM7/9/14
to sage-s...@googlegroups.com
On Tuesday, July 8, 2014 11:53:33 AM UTC-7, Jole Bradbury wrote:
I have a Django project with
views.py:

#!/usr/bin/env sage -python

from django.shortcuts import render
from django.http import HttpResponse
import sys
from django.http import HttpRequest
from django.template import RequestContext, loaders
sys
.path.append('/Users/Jole/Desktop/django_proj/mysite/sage/src/bin')
sys
.path.append('/Users/Jole/Desktop/django_proj/mysite/sage/')
from sage.all import *

Unfortunately, when I fire up my Django server on localhost, I get: No module named sage.all

As an error message. As you can see I have already tried appending to the path. My Python Path includes sage and I can see this on my Django page, however my "PATH" is
PATH
'/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin'


That's not the path that `sage -python` would set up, so my guess is that `views.py` doesn't actually get *executed* (and hence run in a way equivalent to `sage -python views.py`), but instead gets loaded/interpreted by whatever python is running django. You should probably try and execute

sage -python -c 'import django.shortcuts'

my guess is that that would fail, proving that you didn't install django in sage's python and hence prove that your script isn't being executed by sage's python, since that line seems to execute properly in your situation.

If you want to use both django and sage in the same python, you have to ensure that both are available to the same python. The easier way to accomplish that is probably to install django in sage's python, since sage has its own python for a reason. I have no experience with django, nor a clear idea what it does, so I have no idea whether it's a good/feasible plan to mix the two. If django is a web-server, then you should probably tread very carefully. Math software and web services mix badly, because math software is usually written with no concern for security (it's written for a situation where one trusts the user), but for web software security is vital.

Things like sagecell and MathCloud put a *lot* of work in mitigating the security holes that are virtually unavoidable in making large parts of math software available via web services.

Jole Bradbury

unread,
Jul 9, 2014, 11:28:37 AM7/9/14
to sage-s...@googlegroups.com
Aha, that does make sense... And yes the Django import failed. My PATH has Python version 3.4 on it which should have been a dead giveaway since Sage runs Python 2.6? Or 2.7 I believe?

Would you recommend that I attempt to use kcrisman's answer, or try to install Django in Sage's Python, OR should I use sage notebook?

William Stein

unread,
Jul 9, 2014, 12:32:31 PM7/9/14
to sage-support
On Wed, Jul 9, 2014 at 8:28 AM, Jole Bradbury <eghb...@miamioh.edu> wrote:
Aha, that does make sense... And yes the Django import failed. My PATH has Python version 3.4 on it which should have been a dead giveaway since Sage runs Python 2.6? Or 2.7 I believe?

Would you recommend that I attempt to use kcrisman's answer, or try to install Django in Sage's Python, OR should I use sage notebook?

Install Django into Sage's Python... at let us know if it works -- I've never heard of anybody doing it (or even trying):

 sage -sh
 easy_install django  # or something like that...


 


On Wednesday, July 9, 2014 11:18:35 AM UTC-4, Nils Bruin wrote:
On Tuesday, July 8, 2014 11:53:33 AM UTC-7, Jole Bradbury wrote:
I have a Django project with
views.py:

#!/usr/bin/env sage -python

from django.shortcuts import render
from django.http import HttpResponse
import sys
from django.http import HttpRequest
from django.template import RequestContext, loaders
sys
.path.append('/Users/Jole/Desktop/django_proj/mysite/sage/src/bin')
sys
.path.append('/Users/Jole/Desktop/django_proj/mysite/sage/')
from sage.all import *
 
Unfortunately, when I fire up my Django server on localhost, I get: No module named sage.all

As an error message. As you can see I have already tried appending to the path. My Python Path includes sage and I can see this on my Django page, however my "PATH" is
PATH
'/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin'


That's not the path that `sage -python` would set up, so my guess is that `views.py` doesn't actually get *executed* (and hence run in a way equivalent to `sage -python views.py`), but instead gets loaded/interpreted by whatever python is running django. You should probably try and execute

sage -python -c 'import django.shortcuts'

my guess is that that would fail, proving that you didn't install django in sage's python and hence prove that your script isn't being executed by sage's python, since that line seems to execute properly in your situation.

If you want to use both django and sage in the same python, you have to ensure that both are available to the same python. The easier way to accomplish that is probably to install django in sage's python, since sage has its own python for a reason. I have no experience with django, nor a clear idea what it does, so I have no idea whether it's a good/feasible plan to mix the two. If django is a web-server, then you should probably tread very carefully. Math software and web services mix badly, because math software is usually written with no concern for security (it's written for a situation where one trusts the user), but for web software security is vital.

Things like sagecell and MathCloud put a *lot* of work in mitigating the security holes that are virtually unavoidable in making large parts of math software available via web services.

--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
To post to this group, send email to sage-s...@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.



--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org

Jole Bradbury

unread,
Jul 9, 2014, 3:41:24 PM7/9/14
to sage-s...@googlegroups.com
Then how do people set up sage servers? Should I use the notebook?

kcrisman

unread,
Jul 10, 2014, 10:02:12 AM7/10/14
to sage-s...@googlegroups.com


On Wednesday, July 9, 2014 3:41:24 PM UTC-4, Jole Bradbury wrote:
Then how do people set up sage servers? Should I use the notebook?


Typical Sage servers are just public instances of the notebook - see for instance http://wiki.sagemath.org/SageServer though it's a little dated.   You do need to know something about security to use them properly.

On another note I believe you've asked about (though I couldn't find the thread), the Sage cell readme has been updated (https://github.com/sagemath/sagecell/blob/master/README.md) - thanks, Andrey!  I don't know how a few of those commands will change with you using Mac, but the main ones you'd have to replace are wget (I use "curl -0") and instead of apt-get for npm use their Mac installer (but watch out for problems like https://github.com/npm/npm/issues/3125).
Reply all
Reply to author
Forward
0 new messages