Differences between revisions 8 and 9
Revision 8 as of 2008-07-09 08:32:11
Size: 4371
Editor: OndrejCertik
Comment:
Revision 9 as of 2008-07-10 23:32:20
Size: 4379
Editor: GlennTarbox
Comment:
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
{{{ {{{#!python

How to run Sage in a systemwide Python

Easy Way

Create this s.py python script:

   1 import os
   2 import re
   3 import sys
   4 
   5 def set_paths(root="/home/ondra/ext/sage"):
   6     """
   7     Set paths necessary to import sage.
   8 
   9     root ... the path to your Sage installation
  10     """
  11     python_path = [
  12         root+"/local/lib/python",
  13         root+"/local/lib/python2.5/lib-dynload",
  14         root+"/local/lib/python2.5/site-packages",
  15             ]
  16     sys.path = python_path + sys.path
  17 
  18     paths = {
  19             "SAGE_ROOT": root,
  20             "SAGE_LOCAL": root+"/local",
  21             "DOT_SAGE": "~/.sage/",
  22             "SAGE_SERVER": "http://www.sagemath.org/",
  23             "PATH": root+"/local/bin:"+os.environ["PATH"],
  24             }
  25     os.environ.update(paths)
  26 
  27 def import_sageall():
  28     """
  29     Imports sage by "import sage.all".
  30 
  31     If it fails, write a helpful message and exit. After executing this method,
  32     you can be sure that sage just works, e.g you can do
  33 
  34     from sage.all import something
  35 
  36     without worrying.
  37     """
  38     try:
  39         import sage.all
  40     except ImportError, e:
  41         if re.match("(.*).so: cannot open shared object file: No such file or directory", e.message):
  42             # all seems to be just a LD_LIBRARY_PATH problem in 99% of cases
  43             print """\
  44 "import sage.all" failed when importing an .so library, this is most probably
  45 an LD_LIBRARY_PATH problem, please set your LD_LIBRARY_PATH and try again, e.g. execute:
  46 LD_LIBRARY_PATH=%s/local/lib python %s""" % (os.environ.get("SAGE_ROOT", ""),
  47         sys.argv[0])
  48             sys.exit()
  49         else:
  50             # some other problem
  51             raise
  52 
  53 set_paths()
  54 import_sageall()
  55 
  56 from sage.all import *
  57 var("x")
  58 e = x**2/3-log(x)
  59 print e
  60 print integrate(e, x)

and try to run it:

$ python s.py
"import sage.all" failed when importing an .so library, this is most probably
an LD_LIBRARY_PATH problem, please set your LD_LIBRARY_PATH and try again, e.g. execute:
LD_LIBRARY_PATH=/home/ondra/ext/sage/local/lib python s.py

it tells you the exact command that you need to execute to make it work, so do it:

$ LD_LIBRARY_PATH=/home/ondra/ext/sage/local/lib python s.py

                                   2
                                  x
                                  -- - log(x)
                                  3

                                            3
                                           x
                              - x log(x) + -- + x
                                           9

So as you can see from the above, all you have to do is to set the root keyword parameter in the script to point to your Sage installation and that's it. The script will tell you the correct command to run itself.

Robust Way

Create the run_sage bash script:

export SAGE_ROOT=/home/ondra/ext/sage

export SAGE_LOCAL=$SAGE_ROOT/local
export DOT_SAGE=~/.sage/
export PATH=$SAGE_ROOT/local/bin:$PATH

export LD_LIBRARY_PATH="$SAGE_ROOT/local/lib/openmpi:$SAGE_ROOT/local/lib/:"

export PYTHONPATH="$SAGE_ROOT/local/bin:$SAGE_ROOT/local/lib/python2.5/site-packages/setuptools-0.6c8-py2.5.egg:$SAGE_ROOT/local/lib/python2.5/site-packages/SQLAlchemy-0.4.3-py2.5.egg:$SAGE_ROOT/local/bin:$SAGE_ROOT/local/lib/python:$SAGE_ROOT/local/lib/python25.zip:$SAGE_ROOT/local/lib/python2.5/plat-linux2:$SAGE_ROOT/local/lib/python2.5/lib-tk:$SAGE_ROOT/local/lib/python2.5/lib-dynload:$SAGE_ROOT/local/lib/python2.5/site-packages:$SAGE_ROOT/local/lib/python2.5/site-packages/IPython/Extensions"

ipython

Then:

$ ./run_sage 
Python 2.5.1 (r251:54863, Mar 29 2008, 09:29:46) 
Type "copyright", "credits" or "license" for more information.

IPython 0.8.1 -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: from sage.all import *
Falling back to a hard coded sage server in misc/hg.py

In [2]: var("x")                                                               
Out[2]: x

In [3]: x**2/4 + log(x)                                                        
Out[3]: log(x) + x^2/4