Ask Your Question

Python's C API and SAGE

asked 2012-11-09 23:47:05 +0200

rmp251 gravatar image

I have written some python code which imports and uses the sage library. I would like to invoke some of this code from C++ code using the C API for python. The problem is that the C API uses the system-wide python installation, rather than sage's python, and therefore the "from sage.all import *" statement generates ImportError. Can anyone tell me how to
(i) install the sage libraries into the system's python installation, OR
(ii) tell the C API to use sage's python rather than the regular version, OR
(iii) resolve this some other way?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2012-11-10 01:20:01 +0200

You should compile and run your C++ code from the Sage shell. This will set the environment variables necessary to use Python and various libraries installed by Sage.

Invoke sage with the -sh command line argument to get a Sage shell. For example:

burcin@carl ~/sage/sage-5.2 $ ./sage -sh

Starting subshell with Sage environment variables set.  Don't forget
to exit when you are done.  Beware:
 * Do not do anything with other copies of Sage on your system.
 * Do not use this for installing Sage packages using "sage -i" or for
   running "make" at Sage's root directory.  These should be done
   outside the Sage shell.

Bypassing shell configuration files...

Note: SAGE_ROOT=/home/burcin/sage/sage-5.2
(sage-sh) burcin@carl:sage-5.2$

If you are using Gentoo linux, you can also try sage-on-gentoo. There is also lmonade, a meta-distribution (in development) which makes the advantages of a Sage shell usable for software not included in the Sage distribution.

edit flag offensive delete link more


Thanks, this seems to work. Even compiling it outside the sage shell and running it in the sage shell works. Not quite what I was looking for though (I'd prefer if sage didn't need to be the master).

rmp251 gravatar imagermp251 ( 2012-11-10 20:36:01 +0200 )edit

You need to run things in the Sage shell since Sage relies on the LD_LIBRARY_PATH environment variable to find its shared libraries. lmonade ( embeds the location of the libraries in the files themselves with -rpath directives, so there is no need to start a Sage/lmonade shell there. Though, at the moment lmonade needs some love to support the latest Sage version.

burcin gravatar imageburcin ( 2012-11-11 05:41:02 +0200 )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


Asked: 2012-11-09 23:47:05 +0200

Seen: 379 times

Last updated: Nov 10 '12