ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 11 Nov 2012 05:41:02 +0100Python's C API and SAGEhttps://ask.sagemath.org/question/9517/pythons-c-api-and-sage/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<br>
(i) install the sage libraries into the system's python installation, OR<br>
(ii) tell the C API to use sage's python rather than the regular version, OR<br>
(iii) resolve this some other way?
Fri, 09 Nov 2012 23:47:05 +0100https://ask.sagemath.org/question/9517/pythons-c-api-and-sage/Answer by burcin for <p>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<br/>
(i) install the sage libraries into the system's python installation, OR<br/>
(ii) tell the C API to use sage's python rather than the regular version, OR<br/>
(iii) resolve this some other way? </p>
https://ask.sagemath.org/question/9517/pythons-c-api-and-sage/?answer=14244#post-id-14244You 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](https://github.com/cschwan/sage-on-gentoo). There is also [lmonade](http://lmona.de), a meta-distribution (in development) which makes the advantages of a Sage shell usable for software not included in the Sage distribution.Sat, 10 Nov 2012 01:20:01 +0100https://ask.sagemath.org/question/9517/pythons-c-api-and-sage/?answer=14244#post-id-14244Comment by rmp251 for <p>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.</p>
<p>Invoke <code>sage</code> with the <code>-sh</code> command line argument to get a Sage shell. For example:</p>
<pre><code>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$
</code></pre>
<p>If you are using Gentoo linux, you can also try <a href="https://github.com/cschwan/sage-on-gentoo">sage-on-gentoo</a>. There is also <a href="http://lmona.de">lmonade</a>, a meta-distribution (in development) which makes the advantages of a Sage shell usable for software not included in the Sage distribution.</p>
https://ask.sagemath.org/question/9517/pythons-c-api-and-sage/?comment=18729#post-id-18729Thanks, 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).Sat, 10 Nov 2012 20:36:01 +0100https://ask.sagemath.org/question/9517/pythons-c-api-and-sage/?comment=18729#post-id-18729Comment by burcin for <p>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.</p>
<p>Invoke <code>sage</code> with the <code>-sh</code> command line argument to get a Sage shell. For example:</p>
<pre><code>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$
</code></pre>
<p>If you are using Gentoo linux, you can also try <a href="https://github.com/cschwan/sage-on-gentoo">sage-on-gentoo</a>. There is also <a href="http://lmona.de">lmonade</a>, a meta-distribution (in development) which makes the advantages of a Sage shell usable for software not included in the Sage distribution.</p>
https://ask.sagemath.org/question/9517/pythons-c-api-and-sage/?comment=18725#post-id-18725You 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.Sun, 11 Nov 2012 05:41:02 +0100https://ask.sagemath.org/question/9517/pythons-c-api-and-sage/?comment=18725#post-id-18725