Ask Your Question
1

Python's C API and SAGE

asked 12 years ago

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?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 12 years ago

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.

Preview: (hide)
link

Comments

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 ( 12 years ago )

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 (http://www.lmona.de) 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 ( 12 years ago )

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: 12 years ago

Seen: 519 times

Last updated: Nov 10 '12