Can I write a sage program and run it from command line?

3,194 views
Skip to first unread message

Kurda Yon

unread,
May 28, 2009, 6:50:11 PM5/28/09
to sage-support
Hi all,

is it possible to write a file with a "sage program" (a set of sage
command) and then execute the program?

I tried to write a file (called test.py) just with one line:
print 3

Then I typed in the Linux command line:
./sage test.py

It worked! I got 3 as a result.

Then I tried to construct a bit more complicated code:
A = Matrix([[1,2,3],[3,2,1],[1,1,1]])
A.eigenvectors_right()

Unfortunately it did not work. I got the following:
Traceback (most recent call last):
File "test.py", line 1, in <module>
A = Matrix([[1,2,3],[3,2,1],[1,1,1]])
NameError: name 'Matrix' is not defined
sage: Error running test.py using Python

Is it possible to run test.py not by using Python, but as a set of
sage commands?

Thank you in advance.

David Joyner

unread,
May 29, 2009, 7:02:10 AM5/29/09
to sage-s...@googlegroups.com
You might be looking for the attach command:
http://www.sagemath.org/doc/tutorial/programming.html

Laurent

unread,
May 29, 2009, 7:33:48 AM5/29/09
to sage-s...@googlegroups.com
Sage is a python module.

The following is an example which defines a class from which you can
manipulate functions :

-------------------------------------------------

#! /usr/bin/sage -python
# -*- coding: utf8 -*-

from sage.all import *

class MaClasse(object):
def __init__(self,exp):
self.sage = exp
self.sageFast = self.sage._fast_float_(x)
def Derr(self):
return MaClasse(self.sage.derivative())
def Evaluation(self,xe):
return numerical_approx(self.sageFast(xe))


var('x,y')
f = MaClasse(x**2+2)

print f.Derr().sage
print f.Derr().Evaluation(3)
print f.Evaluation(8)


--------------------------------------

Copy-paste it in a text file, make it executable, and enjoy !

Laurent


Kurda Yon

unread,
May 29, 2009, 9:41:57 AM5/29/09
to sage-support
Hi Laurent,

Thank you for your answer. I am trying to understand your code (I
started to use sage just yesterday, so it can be a problem for me).
The first 2 lines looks like you want to execute the commands in as a
shell script. This assumptions is also supported by by a fact that I
have to make the with the code executable. Is my assumption correct.

Next, you write that sage is just a Python module. I guess it means
that I can run sage code like a python code (if I connect the module
in a appropriate way). I assume that the module can be connected by
the following line:
from sage.all import *

But because of some reason it does not work. I create a file called
"test.py". I put there just one line "from sage.all import *" and then
I execute this code as a Python program "python test.py". Then I get
the following:
File "test.py", line 1, in <module>
from sage.all import *
ImportError: No module named sage.all

Anyway, if I do not try to guess and just use the code given by you in
a described (by you) way I get the following:
bash: ./test.sh: /usr/bin/sage: bad interpreter: No such file or
directory

For mode details, I put the code in "test.sh". I make this file
executable "chmod +x test.sh". Then I run it in the shell command line
"./test.sh".

davidloeffler

unread,
May 29, 2009, 9:45:41 AM5/29/09
to sage-support
Another alternative: if you call your file "test.sage" rather than
"test.py", and then do "sage test.sage" from the command line, it will
be run as if you had typed all the corresponding commands in a Sage
session, so you don't need the "from sage.all import *" line in
Laurent's example above.

David

davidloeffler

unread,
May 29, 2009, 9:49:52 AM5/29/09
to sage-support
On May 29, 2:41 pm, Kurda Yon <kurda...@yahoo.com> wrote:
> But because of some reason it does not work. I create a file called
> "test.py". I put there just one line "from sage.all import *" and then
> I execute this code as a Python program "python test.py". Then I get
> the following:
>   File "test.py", line 1, in <module>
>     from sage.all import *
> ImportError: No module named sage.all

Try using "./sage -python test.py". Sage has its own Python
interpreter (which can "see" the rest of Sage as a python library) --
what you typed uses the system Python, which doesn't know about Sage.
Or you can skip the "-python" -- it's clever enough to know that is
meant when it's given a .py file

David

Kurda Yon

unread,
May 29, 2009, 9:54:09 AM5/29/09
to sage-support
Great! It works. It is exactly what I needed. By the way. If I execute
test.sage, sage creates a new file called sage.py. Is there a way to
prevent that?

Laurent

unread,
May 29, 2009, 10:03:46 AM5/29/09
to sage-s...@googlegroups.com
davidloeffler ha scritto:

> Another alternative: if you call your file "test.sage" rather than
> "test.py", and then do "sage test.sage" from the command line, it will
> be run as if you had typed all the corresponding commands in a Sage
> session, so you don't need the "from sage.all import *" line in
> Laurent's example above.
>
In the case where I'm using the test.sage trick, can I still import my
own modules
or have access to everything ?
I guess the answer is Yes, isn't ?

Other point : when I write scripts like the one I posted, the execution
is quite slow, at least, it takes a while before starting. I suppose
that the line
from sage.all import *
takes ages.

Is there a way to import less ?

Have a good afternoon
Laurent


William Stein

unread,
May 29, 2009, 10:11:10 AM5/29/09
to sage-s...@googlegroups.com

No, unfortunately there isn't.

-- William

pang

unread,
May 30, 2009, 11:10:49 AM5/30/09
to sage-support
> In the case where I'm using the test.sage trick, can I still import my
> own modules
> or have access to everything ?
> I guess the answer is Yes, isn't ?
>
I've tried to import both sage and pygtk without success, both in
python and sage console. Please tell me if you can achieve this. I
guess you have to install the module within sage, even if it is
installed in the system, but outside sage.

Regards

Tim Lahey

unread,
May 30, 2009, 11:13:08 AM5/30/09
to sage-s...@googlegroups.com

On May 30, 2009, at 11:10 AM, pang wrote:

> I've tried to import both sage and pygtk without success, both in
> python and sage console. Please tell me if you can achieve this. I
> guess you have to install the module within sage, even if it is
> installed in the system, but outside sage.
>

Sage only knows about python modules installed within its python
installation so any other python modules you want to use with it
have to be installed there.

Cheers,

Tim.

---
Tim Lahey
PhD Candidate, Systems Design Engineering
University of Waterloo
http://www.linkedin.com/in/timlahey

Kevin Horton

unread,
May 30, 2009, 12:55:38 PM5/30/09
to sage-s...@googlegroups.com


One option that I have used is to put *.pth files inside SAGE_ROOT/local/lib/python/site-packages/  Each line in the *.pth file is the path to a directory that will be added to the search path for python modules.

See SAGE_ROOT/local/lib/python/site.py for more info.

Another option is to manually add python module search directories on the fly, by importing the sys module, and appending them to sys.path.  For example, if you were working with a remote sage server, and you did not have access to the file system to install modules, you could upload a module to the DATA directory of a worksheet, then do:

import sys
sys.path.append(DATA)
import your_module

--
Kevin Horton
Ottawa, Canada

Laurent

unread,
May 30, 2009, 1:26:55 PM5/30/09
to sage-s...@googlegroups.com

>> I've tried to import both sage and pygtk without success, both in
>> python and sage console. Please tell me if you can achieve this. I
>> guess you have to install the module within sage, even if it is
>> installed in the system, but outside sage.
>>

The same here :

#! /usr/bin/sage -python
# -*- coding: utf8 -*-

from sage.all import *
import sys # sys n'est utilisé que pour la partie Qt.
from PyQt4 import QtCore, QtGui


produces :

Traceback (most recent call last):

File "./sagess.py", line 6, in <module>
from PyQt4 import QtCore, QtGui
ImportError: No module named PyQt4

...
well. Good to know.

Laurent


simon...@uni-jena.de

unread,
May 30, 2009, 2:40:38 PM5/30/09
to sage-support
Dear Laurent,

I think the idea is that Sage is "with batteries included" and should
not interfere with anything that you have on your system. Namely,
since Sage is rather big, it is very probable that Sage ships
something that is already installed on your computer -- in your case,
Python. So, either the installation of Sage would nuke your Python (I
guess you wouldn't be happy about it...), or during installation, Sage
should try to find things on your system and try to use them. AFAIK,
that would frankly be impossible, in such a complicated system.

On the dark side, you need to re-install all your Python packages. On
the bright side, it shouldn't be difficult. Sage also provides its own
shell, that you can obtain by the command
sage -sh
In the Sage shell, the PATH points to the things that were installed
by Sage. Hence, when you run Python inside the Sage shell, then it is
Sage's Python. Start the Sage shell, install a Python package, and
quit the Sage shell, then afterwards you should be able to use the
package in Sage.

Best regards,
Simon

Robert Bradshaw

unread,
Jun 1, 2009, 2:12:36 PM6/1/09
to sage-s...@googlegroups.com

Note you can also do

sage -python

to run Sage's Python. Thus to install packages, you can do stuff like

sage -python setup.py install

- Robert


Reply all
Reply to author
Forward
0 new messages