Ask Your Question
1

python/sage scripts

asked 2012-10-09 16:52:29 +0100

rmp251 gravatar image

updated 2012-10-09 23:41:51 +0100

First of all, my apologies for being a noob and a windows user. I'm using sage in virtualbox. I'm at this part of the tutorial: sagenb.org/doc/live/tutorial/programming.html#standalone-python-sage-scripts

I'm having 2 problems:

(1) I can't execute "./factor 2006". It says "no such file or directory", but the file is definitely there, and "sage -python factor 2006" works.

(2) factoring polynomials doesn't seem to work for me in sage. If I call factor on a string I get a TypeError: "unable to factor n".

Edit: Part of the problem may have been that the script was in a shared folder in VirtualBox. Here is some exact input/output, working completely locally (this changes the error in (2)).

>./factor 2006

/usr/bin/env: sage -python: No such file or directory

>sage -python factor "32*x^5-1"

NameError: name 'x' is not defined

Any assistance is much appreciated!

edit retag flag offensive close merge delete

Comments

Hi! I can guess what your problems are, but it would be better if you posted *exact commands* that you tried that didn't work. That makes it easier for others to be helped by your question too!

kcrisman gravatar imagekcrisman ( 2012-10-09 17:37:33 +0100 )edit

See my edit, thanks.

rmp251 gravatar imagermp251 ( 2012-10-09 23:48:49 +0100 )edit

Thanks. Gee, this really suggests that `from sage.all import *` isn't being done, not to mention the enigmatic no such file problem.

kcrisman gravatar imagekcrisman ( 2012-10-09 23:55:26 +0100 )edit

4 Answers

Sort by » oldest newest most voted
2

answered 2012-10-12 23:15:05 +0100

updated 2012-10-12 23:19:54 +0100

Edit: try replacing the first line with #!/usr/bin/env sage. That might bypass all of the problems described below.

One more attempt at an answer: perhaps the shell in VirtualBox doesn't accept the syntax #!/usr/bin/env sage -python — some shells balk at having more than one term after "/usr/bin/env". So changing this first line to #!/usr/bin/env python and executing it with sage -sh -c './factor 2012' should work, as should first running sage -sh and then ./factor 2012. In either case, sage -sh tells /usr/bin/env python to use Sage's version of Python, which knows how to import the Sage library (from the line from sage.all import *).

That's my guess. See Wikipedia's article on 'Shebang (Unix)' – I don't know how to add the link here, since the link has a right parenthesis in it, which conflicts with the markup here for links – and in particular the part which says

Another portability problem is the interpretation of the command arguments. Some systems, including Linux, do not split up the arguments;[11] for example, when running the script with the first line like,

`#!/usr/bin/env python -c`

That is, python -c will be passed as one argument to /usr/bin/env, rather than two arguments. Cygwin also behaves this way.

While the original script (with #!/usr/bin/env sage -python) works on my OS X box, it doesn't work on a linux machine that I have access to. This part of the tutorial needs to be fixed, I think.

edit flag offensive delete link more

Comments

See http://trac.sagemath.org/sage_trac/ticket/13597 for a ticket tracking this problem.

John Palmieri gravatar imageJohn Palmieri ( 2012-10-12 23:21:39 +0100 )edit

Thanks a lot - this works. (As to WHY it works I'm still confused... I guess I don't understand the meaning of the hash-bang and /usr/bin/env)

rmp251 gravatar imagermp251 ( 2012-10-15 14:10:42 +0100 )edit

"#!/usr/bin/env sage" as the first line in the script says what program should be used to process the script, in this case "/usr/bin/env sage", so it is equivalent to running "sage factor". Some programs might start with "#!/usr/bin/python", for example, but it's more portable to use "#!/usr/bin/env python" -- the first only finds python if it's in /usr/bin, while the second finds python if it's anywhere in the user's PATH. In this case, the documentation suggested using "#!/usr/bin/env sage -python", to use Sage's installation of Python, but Linux apparently doesn't handle having more than one word after "#!/usr/bin/env" very well.

John Palmieri gravatar imageJohn Palmieri ( 2012-10-15 15:07:09 +0100 )edit

I see - thanks.

rmp251 gravatar imagermp251 ( 2012-10-15 17:19:42 +0100 )edit
0

answered 2012-10-09 19:20:51 +0100

It's possible that the file is there but doesn't have the right permissions: it isn't executable. You could try

$ chmod a+x factor
$ ./factor 2006

and see if that works.

edit flag offensive delete link more

Comments

Thanks for the suggestion - I tried it and got an error (see my edit please).

rmp251 gravatar imagermp251 ( 2012-10-09 23:48:13 +0100 )edit
0

answered 2012-10-09 17:41:37 +0100

kcrisman gravatar image

Here's a link that everyone can access: the tutorial on standalone scripts.

1) Is it possible that you aren't in the appropriate directory? Just a guess. What happens if you type ls? Sorry if this is too naive of question.

2) Strings can't be factored; polynomials can.

sage: P = 'x^2+x'
sage: P.factor()
---------------------------------------------------------------------------
AttributeError: 'str' object has no attribute 'factor'
sage: (x^2+x).factor()
(x + 1)*x
sage: R.<y> = ZZ[]
sage: R
Univariate Polynomial Ring in y over Integer Ring
sage: (y^2+y).factor()
y * (y + 1)

Of course, I didn't get your error message, so maybe something else is going on.

edit flag offensive delete link more

Comments

Thanks for the response. 1) I'm in the appropriate directory and ls verifies that. 2) I see. I guess it is passed as a string argument but then is converted to an expression for sage. Please see my edit. Since the tutorial is for "standalone scripts" I would expect this to work.

rmp251 gravatar imagermp251 ( 2012-10-09 23:43:12 +0100 )edit

Yes, see my comment - `from sage.all import *` should make `x` predefined as a variable.

kcrisman gravatar imagekcrisman ( 2012-10-09 23:55:53 +0100 )edit
0

answered 2012-10-10 14:55:51 +0100

Given the error message in

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

It looks like "sage" can't be found, so I would guess that you haven't followed this direction from the tutorial:

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

I don't know how to do this using VirtualBox, but it might be this: find the directory SAGE_ROOT where sage is; say it's "/home/user/sage-5.3/". Then do

> export PATH=/home/user/sage-5.3/:$PATH

(There should be exactly one space in this line, between "export" and "PATH".) Then try

> ./factor 2006

again.

edit flag offensive delete link more

Comments

sage is in the path: > echo $PATH /home/sage/sage:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin

rmp251 gravatar imagermp251 ( 2012-10-11 23:17:02 +0100 )edit

From the command line, what does "which sage" say?

John Palmieri gravatar imageJohn Palmieri ( 2012-10-12 11:32:13 +0100 )edit

~/sage/sage which I believe is the same thing as /home/sage/sage/sage

rmp251 gravatar imagermp251 ( 2012-10-12 11:47:48 +0100 )edit

What happens if you replace first line of "factor" with "/usr/bin/env python" and then do "sage -sh -c ./factor"?

John Palmieri gravatar imageJohn Palmieri ( 2012-10-12 12:20:02 +0100 )edit

That seems to work, although I can't seem to pass in a parameter to factor that way. Now when I just do ./factor 2006, for example, I get ImportError: No module named sage.all

rmp251 gravatar imagermp251 ( 2012-10-12 14:13:22 +0100 )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

Stats

Asked: 2012-10-09 16:52:29 +0100

Seen: 3,904 times

Last updated: Oct 12 '12