Using Sage in a Python CGI script

i like this post (click again to cancel)
2
i dont like this post (click again to cancel)

I am writing cgi scripts in python that use sage. I have been researching for weeks and even the python and sage websites say use: "from sage.all import *" in your python script, and have "#!/usr/bin/env sage -python" as the first line of the script.

Here is the exact quote from sagemath faq on what to do: " The following standalone Sage script factors integers, polynomials, etc:

#!/usr/bin/env sage -python

import sys
from sage.all import *

if len(sys.argv) != 2:
    print "Usage: %s <n>"%sys.argv[0]
    print "Outputs the prime factorization of n."
    sys.exit(1)

print factor(sage_eval(sys.argv[1]))

In order to use this script, your SAGE_ROOT must be in your PATH."

I made the cgi file exactly as is, and put onto my web server (Apache running CentOS), with sage and python installed. When I try to run this file (named factor.cgi) through the bash terminal I do:

$ ./factor.cgi 2006
/usr/bin/env: sage -python: No such file or directory

Any help on what I should do to make this script work would be appreciated.

asked Jul 07 '11

randomness431 gravatar image randomness431
21 1 1 3

updated Jul 07 '11

Mike Hansen gravatar image Mike Hansen flag of United States
3880 23 46 84

2 Answers:

i like this answer (click again to cancel)
1
i dont like this answer (click again to cancel)

The problem seems to be that sage isn't in your $PATH. You can use thsese instructions to add $SAGE_ROOT to your path. Another (easier, in some sense) solution is to make the first line of your script be

#!/path/to/your/sage-4.7.1/sage -python

and then the script will know explicitly which Sage installation to use.

link

posted Jul 07 '11

Mike Hansen gravatar image Mike Hansen flag of United States
3880 23 46 84
Setting the path works, but that requires me to add a shell script to call this script. I tried your suggestion of putting #!path/to/sage -python as the first line instead and running it as ./factor.cgi 2006 and I get this now: ./factor.cgi: line 2: import: command not found ./factor.cgi: line 3: import: command not found ./factor.cgi: line 5: syntax error near unexpected token `from' ./factor.cgi: line 5: `from sage.all import *' randomness431 (Jul 08 '11)
That's strange. Where is your Sage installed? Could you post your factor.cgi script on something like pastebin.com? My Sage is installed at "/opt/sage", so I'd put "#!/opt/sage/sage -python" at the top of my script. Mike Hansen (Jul 09 '11)
i like this answer (click again to cancel)
1
i dont like this answer (click again to cancel)

Unfortunately, Linux passes the entire rest of the shebang command line as a single argument to the shebang executable. Therefore, if you use "#!/usr/bin/env prog", then it's impossible to pass any arguments to prog.

My solution was to change it to

#!/usr/bin/env sage-python

and write a wrapper script called sage-python containing

exec /home/sage/sage-5.7-linux-32bit-ubuntu_12.04.2_lts-i686-Linux/sage -python $@
link

posted May 29 '13

Curt gravatar image Curt
11 1 2

updated May 29 '13

Your answer

Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
Login/Signup to Post

Question tools

Tags:

Stats:

Asked: Jul 07 '11

Seen: 1,029 times

Last updated: May 29 '13

powered by ASKBOT version 0.7.22
Copyright Sage, 2010. Some rights reserved under creative commons license.