Ask Your Question
3

Using Sage in a Python CGI script

asked 2011-07-07 17:19:00 +0200

randomness431 gravatar image

updated 2011-07-07 18:07:10 +0200

Mike Hansen gravatar image

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.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2011-07-07 18:12:58 +0200

Mike Hansen gravatar image

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.

edit flag offensive delete link more

Comments

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 gravatar imagerandomness431 ( 2011-07-08 10:00:11 +0200 )edit

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 gravatar imageMike Hansen ( 2011-07-09 06:35:57 +0200 )edit
1

answered 2013-05-29 15:40:32 +0200

Curt gravatar image

updated 2013-05-29 15:46:11 +0200

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 $@
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2011-07-07 17:19:00 +0200

Seen: 8,918 times

Last updated: May 29 '13