Problems with standalone python/sage scripts

365 views
Skip to first unread message

Georg

unread,
Jan 15, 2008, 12:52:26 PM1/15/08
to sage-support
Hi,
Using sage-2.9.3, on Debian Etch on a core duo machine.
According to the sage tutorial p.67 section 5.3 Standalone Python/Sage
Scripts, i tried to run such a script called BMV.sage:

#!/usr/bin/env sage -python
import sys

$ ./BMV.sage
/usr/bin/env: sage -python: file or directory not found

though
$ /usr/bin/env sage -python
brings me to the python command line, i.e. this works.

changing the file BMV.sage to:

#!/path/to/sage/sage -python
import sys

shows strange behaviour:
if i run this script, nothing happens, just the mouse pointer changes
(into a cross), clicking the mouse button (left or right) brings back
the shell command line prompt, no output at all...., and the mouse
pointer turns to normal again...

runnin the script:

#!/path/to/sage/sage -python
a = "Hello"

outputs
./BMV.sage: line 2: a: command not found

the script from the tutorial, literally:

#!/home/georg/Daten/.System/bin/sage/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])

gives the following output, as above not before clicking one of the
mouse buttons:
$ ./BMV.sage
from: can't read /var/mail/sage.all
./BMV.sage: line 4: syntax error near unexpected token `sys.argv'
./BMV.sage: line 4: `if len(sys.argv) != 2:'

additionally there is a file called 'sys' created in the directory of
BMV.sage, this is to long to post it here, the beginning is

%!PS-Adobe-3.0
%%Creator: (ImageMagick)
%%Title: (sys)
%%CreationDate: (Tue Jan 15 18:43:48 2008)
%%BoundingBox: 0 0 897 433
%%HiResBoundingBox: 0 0 897 433
%%DocumentData: Clean7Bit
%%LanguageLevel: 1
%%Orientation: Portrait
%%PageOrder: Ascend
%%Pages: 1
%%EndComments
.....

thanks for help, Georg

Robert Miller

unread,
Jan 15, 2008, 4:38:58 PM1/15/08
to sage-support
Georg,

1. Is this a clean, from-source build of sage-2.9.3?

2. What is the output of /usr/bin/env for you? Mine (Intel OS X
10.5.1) doesn't mention sage at all, although mysteriously things are
working for me.

> #!/usr/bin/env sage -python
> import sys

For me, I get
$ ./BMV.sage
$

Also, if I add print 2+2, it works too:
$ ./BMV.sage
4

However, when I run the sage itself, it looks like it's pointing at
some weird version:
$ /usr/bin/env sage
----------------------------------------------------------------------
| SAGE Version 2.9.2, Release Date: 2008-01-05 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
Loading SAGE library. Current Mercurial branch is: demo

This isn't what I was expecting at all (I just recently started using
this particular laptop). After some digging, I discover that my /usr/
local/bin contains a script called "sage", which seems to be pretty
much a copy of the main script "sage", in the root directory of a
typical sage install, but the SAGE_ROOT is explicitly set to another
version of sage.

So here's what I did:

$ export PATH=/Users/rlmill/sage:$PATH
(this explicitly points to the version I want)
$ ./test
SAGE Version 2.9.3, Release Date: 2008-01-05
(hooray - this is what I was hoping for)


The contents of test are: (minus triple quotes)
"""
#!/usr/bin/env sage -python
import sage
import sage.misc
import sage.misc.banner
from sage.misc.banner import version
print version()
"""

Hopefully this helps, although I have a feeling this thread isn't
over...

-- Robert M

Georg Grafendorfer

unread,
Jan 15, 2008, 6:00:34 PM1/15/08
to sage-s...@googlegroups.com
Hi Robert,

1. Is this a clean, from-source build of sage-2.9.3?

Yes, i tried it out on two different systems now, Athlon XP, and Core Duo, both running on Debian Etch, and both show the same behaviour

2. What is the output of /usr/bin/env for you? Mine (Intel OS X
10.5.1) doesn't mention sage at all, although mysteriously things are
working for me.

the  SAGE_ROOT directory is included in my path, i even defined a global variable with this name, i.e.  SAGE_ROOT.
 

> #!/usr/bin/env sage -python
> import sys

For me, I get
$ ./BMV.sage
$

you response encouraged me to try out some more things, and if i change the first line to
#!/usr/bin/env sage-python
instead of
#!/usr/bin/env sage -python
(note that there is no space anymore) things work as excepted (at least import sys and print "Hello World"), seems like my /usr/bin/env does not like the space between (#!/usr/bin/env "sage -python" does not work either)

but however, using

#!/path/to/sage_root/sage-python
import sys

still does not work and shows the same strange "mouse behaviour" as described in my original posting, on both systems!!
no idea why the second one does not work on my systems!!


However, when I run the sage itself, it looks like it's pointing at
some weird version:
$ /usr/bin/env sage
----------------------------------------------------------------------
| SAGE Version 2.9.2, Release Date: 2008-01-05                       |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
Loading SAGE library. Current Mercurial branch is: demo

$/usr/bin/env sage

brings me to the sage prompt as expected

Hopefully this helps, although I have a feeling this thread isn't
over...

Anyway, at least i found a partial solution to carry on with, but it seems as there are still some things to clarify, especially the mouse thing concerning the import sys

Thank you very much,
Georg


William Stein

unread,
Jan 15, 2008, 6:40:48 PM1/15/08
to sage-s...@googlegroups.com
On Jan 15, 2008 3:00 PM, Georg Grafendorfer

I consider all this a bug, and it's definitely a problem on numerous _linux_
systems. This problem doesn't happen at all on OSX. I've made it
http://trac.sagemath.org/sage_trac/ticket/1789

William

mabshoff

unread,
Jan 15, 2008, 8:33:02 PM1/15/08
to sage-support
I thought I have seen this before and the issue was a buggy env, i.e.

#!/usr/bin/env sage -python

while

#!/usr/bin/env sage

should work. There was a thread about this in one of the sage-* Google
groups. It boiled down to that env should work fine with multi
arguments while some buggy env can't handle it. Maybe we can add some
script that does the equivalent of "sage -python", maybe "local/bin/
sage-python" does that job, but I assume the env isn't set up
properly. In that case we should change the documentation since it has
been popping up over and over these days.

> William

Cheers,

Michael

Georg Grafendorfer

unread,
Jan 16, 2008, 3:56:46 AM1/16/08
to sage-s...@googlegroups.com

I thought I have seen this before and the issue was a buggy env, i.e.

 Maybe we can add some
script that does the equivalent of "sage -python", maybe "local/bin/
sage-python" does that job, but I assume the env isn't set up
properly.

#!/usr/bin/sage-python

works, i mentioned this in my previous posting,

and

#!/path/to/sage_root/sage-python

does not work, but maybe that's not important,

Georg


Robert Miller

unread,
Jan 17, 2008, 1:05:37 PM1/17/08
to sage-support
Georg,

> #!/path/to/sage_root/sage-python
>
> does not work, but maybe that's not important,

Does

#!/path/to/sage_root/local/bin/sage-python

work?

Georg Grafendorfer

unread,
Jan 18, 2008, 3:09:52 PM1/18/08
to sage-s...@googlegroups.com
Hi Robert, excuse me for the response delay, i did not notice your question,

Does

#!/path/to/sage_root/local/bin/sage-python

work?
no, the output is in both cases (sage -python as well as sage-python):
bash: ./exp1.sage: /home/georg/Daten/.System/bin/sage/local/bin: bad interpreter: Keine Berechtigung
where "Keine Berechtigung = no permission"
it doesn't even work as root, but the file is executable...

Georg

Georg Grafendorfer

unread,
Jan 18, 2008, 3:17:11 PM1/18/08
to sage-s...@googlegroups.com
Hi again,

in my previous posting i had the lines
#!/path/to/sage_root/local/bin sage-python
#!/path/to/sage_root/local/bin sage- python

instead of your request:
#!/path/to/sage_root/local/bin/sage-python

this gives me the same strange mouse effect as described in the original posting, whereas

#!/path/to/sage_root/local/bin/sage -python

gives
bash: ./exp1.sage: /home/georg/Daten/.System/bin/sage/local/bin/sage: bad interpreter: Datei oder Verzeichnis nicht gefunden

Datei oder Verzeichnis nicht gefunden = file or directory not found,
i.e the same behauviour as without the "local/bin"

Georg

mabshoff

unread,
Jan 18, 2008, 3:18:31 PM1/18/08
to sage-support


On Jan 18, 9:09 pm, "Georg Grafendorfer"
What file system is home one? What file system in /home/georg/Daten
on? How do you mount them, i.e. with what options? Check /proc/mounts
for that. Does any script work from that particular location?

Can you run the script with "strace -f ./BMV.sage" and post the
output? That might be long, so please consider tarring up the result
and posting a link.

> Georg

Cheers,

Michael

Georg Grafendorfer

unread,
Jan 18, 2008, 4:00:46 PM1/18/08
to sage-s...@googlegroups.com
Michael,
OK, i'm already a bit confused, and i don't know any more what your requesting exactly, so i will repeat some things:

The file with the name ./example.sage
#!/home/georg/Daten/.System/bin/sage/sage -python
import sys

gives me that strange mouse behaviour where the mouse pointer changes to a cross (but not this "X-cross" known from pure X-Window) and clicking on the 3 mouse buttons brings me back to the command line with no output at all, this is because of the "import sys" line, without this line nothing happens (just back to the next command line prompt).

OK, this is my /etc/fstab:

# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
/dev/sda5       /               ext3    defaults,errors=remount-ro 0       1
/dev/sda6       /home           ext3    defaults        0       2
/dev/sda3       none            swap    sw              0       0
/dev/hdb        /media/cdrom0   udf,iso9660 user,noauto     0       0

this is my /proc/mounts:
rootfs / rootfs rw 0 0
none /sys sysfs rw 0 0
none /proc proc rw 0 0
udev /dev tmpfs rw 0 0
/dev/sda5 / ext3 rw,data=ordered 0 0
/dev/sda5 /dev/.static/dev ext3 rw,data=ordered 0 0
tmpfs /lib/init/rw tmpfs rw,nosuid 0 0
usbfs /proc/bus/usb usbfs rw,nosuid,nodev,noexec 0 0
tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
devpts /dev/pts devpts rw,nosuid,noexec 0 0
/dev/sda6 /home ext3 rw,data=ordered 0 0
/dev/sdb1 /media/usbdisk vfat rw,nosuid,nodev,noexec,uid=1000,gid=1000,fmask=0077,dmask=0077,codepage=cp437,iocharset=utf8,shortname=mixed,quiet 0 0

i.e, my data partition where also sage_root is on is mounted at /home

the output of "strace -f ./example.sage" is:
execve("./exp1.sage", ["./exp1.sage"], [/* 25 vars */]) = -1 ENOEXEC (Exec format error)
dup(2)                                  = 3
fcntl64(3, F_GETFL)                     = 0x2 (flags O_RDWR)
fstat64(3, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f8c000
_llseek(3, 0, 0xbfbad584, SEEK_CUR)     = -1 ESPIPE (Illegal seek)
write(3, "strace: exec: Exec format error\n", 32strace: exec: Exec format error
) = 32
close(3)                                = 0
munmap(0xb7f8c000, 4096)                = 0
exit_group(1)                           = ?
Process 29223 detached

that's all, i hope this is what you expected me to do,
please don't hesitate to ask me for more,
thanks, Georg

Ondrej Certik

unread,
Jan 19, 2008, 1:38:42 AM1/19/08
to sage-s...@googlegroups.com
On Jan 18, 2008 10:00 PM, Georg Grafendorfer

<georg.gra...@gmail.com> wrote:
> Michael,
> OK, i'm already a bit confused, and i don't know any more what your
> requesting exactly, so i will repeat some things:
>
> The file with the name ./example.sage
> #!/home/georg/Daten/.System/bin/sage/sage -python
> import sys
>
> gives me that strange mouse behaviour where the mouse pointer changes to a
> cross (but not this "X-cross" known from pure X-Window) and clicking on the
> 3 mouse buttons brings me back to the command line with no output at all,
> this is because of the "import sys" line, without this line nothing happens
> (just back to the next command line prompt).

This made me curious, becuase I also use Debian (sid) and I get the
exact same behaviour
as you do and I was able to figure out where the problem is in the
last 10 minutes. Need to go now.

But I smell I am going to learn something new here. :)

Ondrej

Ondrej Certik

unread,
Jan 19, 2008, 1:39:22 AM1/19/08
to sage-s...@googlegroups.com

I mean - I wasn't able to figure it out.

O.

Ondrej Certik

unread,
Jan 19, 2008, 1:57:17 AM1/19/08
to sage-s...@googlegroups.com
On Jan 19, 2008 7:38 AM, Ondrej Certik <ond...@certik.cz> wrote:
> On Jan 18, 2008 10:00 PM, Georg Grafendorfer
> <georg.gra...@gmail.com> wrote:
> > Michael,
> > OK, i'm already a bit confused, and i don't know any more what your
> > requesting exactly, so i will repeat some things:
> >
> > The file with the name ./example.sage
> > #!/home/georg/Daten/.System/bin/sage/sage -python
> > import sys
> >
> > gives me that strange mouse behaviour where the mouse pointer changes to a
> > cross (but not this "X-cross" known from pure X-Window) and clicking on the
> > 3 mouse buttons brings me back to the command line with no output at all,
> > this is because of the "import sys" line, without this line nothing happens
> > (just back to the next command line prompt).
>
> This made me curious, becuase I also use Debian (sid) and I get the
> exact same behaviour
> as you do and I wasn't able to figure out where the problem is in the

> last 10 minutes. Need to go now.


ok, this works:

$ cat t.py
#!/bin/sh /home/ondra/ext/sage/sage
import sys
$ ./t.py
$

And this doesn't:

$ cat t.py
#!/home/ondra/ext/sage/sage
import sys
$ ./t.py

(I get the cross - which means that it's bash, who is executing that file btw)

$ cat t.py
#!/bin/sh /home/ondra/ext/sage/sage -python
import sys
$ ./t.py
/bin/sh: /home/ondra/ext/sage/sage -python: No such file or directory


So I don't know. But it must be something stupid - I bet this used to
work for me before.

Ondrej

bill purvis

unread,
Jan 19, 2008, 5:25:17 AM1/19/08
to sage-s...@googlegroups.com
On Saturday 19 January 2008, Ondrej Certik wrote:
> On Jan 19, 2008 7:38 AM, Ondrej Certik <ond...@certik.cz> wrote:
> > On Jan 18, 2008 10:00 PM, Georg Grafendorfer
> >
> > <georg.gra...@gmail.com> wrote:
> > > Michael,
> > > OK, i'm already a bit confused, and i don't know any more what your
> > > requesting exactly, so i will repeat some things:
> > >
> > > The file with the name ./example.sage
> > > #!/home/georg/Daten/.System/bin/sage/sage -python
> > > import sys
> > >
> > > gives me that strange mouse behaviour where the mouse pointer changes
> > > to a cross (but not this "X-cross" known from pure X-Window) and
> > > clicking on the 3 mouse buttons brings me back to the command line with
> > > no output at all, this is because of the "import sys" line, without
> > > this line nothing happens (just back to the next command line prompt).
> >
> > This made me curious, becuase I also use Debian (sid) and I get the
> > exact same behaviour
> > as you do and I wasn't able to figure out where the problem is in the
> > last 10 minutes. Need to go now.
>
> ok, this works:
>
Case 1:

> $ cat t.py
> #!/bin/sh /home/ondra/ext/sage/sage
> import sys
> $ ./t.py
> $
>
> And this doesn't:
>
Case 2:

> $ cat t.py
> #!/home/ondra/ext/sage/sage
> import sys
> $ ./t.py
>
> (I get the cross - which means that it's bash, who is executing that file
> btw)
>
Case 3:

> $ cat t.py
> #!/bin/sh /home/ondra/ext/sage/sage -python
> import sys
> $ ./t.py
> /bin/sh: /home/ondra/ext/sage/sage -python: No such file or directory
>
>
> So I don't know. But it must be something stupid - I bet this used to
> work for me before.
>
> Ondrej
I just tried this under Ubuntu.
Case 1 works

Case 2 outputs
./t.py: line 2: import: command not found

Case 3 behaves as yours.

My 2p: My understanding of the #! convention is that the line is split
up into three parts:
#!
<program name>
<argument(s)>
The <program name> must be an executable PROGRAM, not a script.
The <argument(s) gets treated as a single string.

This would explain my behaviour. Don't know what Debian does in case 2,
Ubuntu seems to just ignore the first line if it's not a true executable
and executes the rest of the file with /bin/sh.

Bill
--
+---------------------------------------+
| Bill Purvis, Amateur Mathematician |
| email: b...@beeb.net |
| http://bil.members.beeb.net |
+---------------------------------------+

William Stein

unread,
Jan 19, 2008, 8:57:53 AM1/19/08
to sage-s...@googlegroups.com

I just posted a patch related to this discussion here
http://trac.sagemath.org/sage_trac/ticket/1789

I think the upshot of the entire discussion above is (or should be) to use

#!/usr/bin/env sage

instead of

#!/usr/bin/env sage -python

This assumes sage is in your path -- and indeed it should be if you want
to write portable scripts.

The second form above, e.g.,


#!/usr/bin/env sage -python

only works on OS X. The first form, e.g.,

#!/usr/bin/env sage

works on both OS X and linux. Both are equivalent in behavior on OS X.

-- william

Georg

unread,
Jan 19, 2008, 1:52:03 PM1/19/08
to sage-support


On 19 Jan., 14:57, "William Stein" <wst...@gmail.com> wrote:

> I think the upshot of the entire discussion above is (or should be) to use
>
> #!/usr/bin/env sage
>
> instead of
>
> #!/usr/bin/env sage -python
>
> This assumes sage is in your path -- and indeed it should be if you want
> to write portable scripts.
>
> The second form above, e.g.,
>
> #!/usr/bin/env sage -python
>
> only works on OS X. The first form, e.g.,
>
> #!/usr/bin/env sage
>
> works on both OS X and linux. Both are equivalent in behavior on OS X.

mmhhh..., you may have a look at the sage-support thread called
"exponentiation bug?", according to this thread
sage -python refers to the python version of sage as opposed to the
system's version of python and nothing else, this sounds Ok, but from
"#!/usr/bin/env sage" i expect 2^8 to be wrapped to 2**8, or?
typing "/usr/bin/env sage" on the shell command line brings me to the
sage prompt whereas "/usr/bin/env sage-python" brings me to the python
promp...., so in OS X both commands bring you to the same prompt?

Georg
Reply all
Reply to author
Forward
0 new messages