ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 10 Jan 2016 08:53:04 -0600Running Sage inside Pythonhttps://ask.sagemath.org/question/32126/running-sage-inside-python/ I was following a [tutorial](http://doc.sagemath.org/html/en/tutorial/programming.html#standalone-python-sage-scripts). I tried to run this code, but got sage is not recognized error. I am using Ubuntu (Mint). I added the sage root to my path. Now when I print $PATH in the terminal there is the sage path indeed. However, my script still does not recognize the Sage package. What may I be doing wrong? If any additional info is needed I will add it.
Here is the output of running $PATH:
> bash:
> /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/goran/Sage/sage-6.9-x86_64-Linux/sage
I am wondering what my header should look like (of the Python script). In the given example it is
#!/usr/bin/env sage.
I have been trying to adapt some other answers but none worked for me. What I want to achieve is calling Sage in Python code, because I have already written a couple of Python modules, and no I just need to call some Sage functions.
Thank you for the understanding! Sat, 09 Jan 2016 11:25:28 -0600https://ask.sagemath.org/question/32126/running-sage-inside-python/Comment by giliev for <p>I was following a <a href="http://doc.sagemath.org/html/en/tutorial/programming.html#standalone-python-sage-scripts">tutorial</a>. I tried to run this code, but got sage is not recognized error. I am using Ubuntu (Mint). I added the sage root to my path. Now when I print $PATH in the terminal there is the sage path indeed. However, my script still does not recognize the Sage package. What may I be doing wrong? If any additional info is needed I will add it.</p>
<p>Here is the output of running $PATH:</p>
<blockquote>
<p>bash:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/goran/Sage/sage-6.9-x86_64-Linux/sage</p>
</blockquote>
<p>I am wondering what my header should look like (of the Python script). In the given example it is </p>
<pre><code>#!/usr/bin/env sage.
</code></pre>
<p>I have been trying to adapt some other answers but none worked for me. What I want to achieve is calling Sage in Python code, because I have already written a couple of Python modules, and no I just need to call some Sage functions. </p>
<p>Thank you for the understanding! </p>
https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32136#post-id-32136I did not use the right words. By writing $PATH I wanted to show how my PATH looks like.Sat, 09 Jan 2016 13:24:54 -0600https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32136#post-id-32136Comment by vdelecroix for <p>I was following a <a href="http://doc.sagemath.org/html/en/tutorial/programming.html#standalone-python-sage-scripts">tutorial</a>. I tried to run this code, but got sage is not recognized error. I am using Ubuntu (Mint). I added the sage root to my path. Now when I print $PATH in the terminal there is the sage path indeed. However, my script still does not recognize the Sage package. What may I be doing wrong? If any additional info is needed I will add it.</p>
<p>Here is the output of running $PATH:</p>
<blockquote>
<p>bash:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/goran/Sage/sage-6.9-x86_64-Linux/sage</p>
</blockquote>
<p>I am wondering what my header should look like (of the Python script). In the given example it is </p>
<pre><code>#!/usr/bin/env sage.
</code></pre>
<p>I have been trying to adapt some other answers but none worked for me. What I want to achieve is calling Sage in Python code, because I have already written a couple of Python modules, and no I just need to call some Sage functions. </p>
<p>Thank you for the understanding! </p>
https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32133#post-id-32133What do you mean by "running $PATH"? It is a bash variable. There is no way to "run it".Sat, 09 Jan 2016 13:12:58 -0600https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32133#post-id-32133Comment by vdelecroix for <p>I was following a <a href="http://doc.sagemath.org/html/en/tutorial/programming.html#standalone-python-sage-scripts">tutorial</a>. I tried to run this code, but got sage is not recognized error. I am using Ubuntu (Mint). I added the sage root to my path. Now when I print $PATH in the terminal there is the sage path indeed. However, my script still does not recognize the Sage package. What may I be doing wrong? If any additional info is needed I will add it.</p>
<p>Here is the output of running $PATH:</p>
<blockquote>
<p>bash:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/goran/Sage/sage-6.9-x86_64-Linux/sage</p>
</blockquote>
<p>I am wondering what my header should look like (of the Python script). In the given example it is </p>
<pre><code>#!/usr/bin/env sage.
</code></pre>
<p>I have been trying to adapt some other answers but none worked for me. What I want to achieve is calling Sage in Python code, because I have already written a couple of Python modules, and no I just need to call some Sage functions. </p>
<p>Thank you for the understanding! </p>
https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32134#post-id-32134Note that there should not be a point in "#!/usr/bin/env sage". In the thing you wrote above there is a point at the end.Sat, 09 Jan 2016 13:17:12 -0600https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32134#post-id-32134Comment by vdelecroix for <p>I was following a <a href="http://doc.sagemath.org/html/en/tutorial/programming.html#standalone-python-sage-scripts">tutorial</a>. I tried to run this code, but got sage is not recognized error. I am using Ubuntu (Mint). I added the sage root to my path. Now when I print $PATH in the terminal there is the sage path indeed. However, my script still does not recognize the Sage package. What may I be doing wrong? If any additional info is needed I will add it.</p>
<p>Here is the output of running $PATH:</p>
<blockquote>
<p>bash:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/goran/Sage/sage-6.9-x86_64-Linux/sage</p>
</blockquote>
<p>I am wondering what my header should look like (of the Python script). In the given example it is </p>
<pre><code>#!/usr/bin/env sage.
</code></pre>
<p>I have been trying to adapt some other answers but none worked for me. What I want to achieve is calling Sage in Python code, because I have already written a couple of Python modules, and no I just need to call some Sage functions. </p>
<p>Thank you for the understanding! </p>
https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32131#post-id-32131Why do you need to do "sudo"? What about just "sage"?Sat, 09 Jan 2016 13:02:13 -0600https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32131#post-id-32131Comment by giliev for <p>I was following a <a href="http://doc.sagemath.org/html/en/tutorial/programming.html#standalone-python-sage-scripts">tutorial</a>. I tried to run this code, but got sage is not recognized error. I am using Ubuntu (Mint). I added the sage root to my path. Now when I print $PATH in the terminal there is the sage path indeed. However, my script still does not recognize the Sage package. What may I be doing wrong? If any additional info is needed I will add it.</p>
<p>Here is the output of running $PATH:</p>
<blockquote>
<p>bash:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/goran/Sage/sage-6.9-x86_64-Linux/sage</p>
</blockquote>
<p>I am wondering what my header should look like (of the Python script). In the given example it is </p>
<pre><code>#!/usr/bin/env sage.
</code></pre>
<p>I have been trying to adapt some other answers but none worked for me. What I want to achieve is calling Sage in Python code, because I have already written a couple of Python modules, and no I just need to call some Sage functions. </p>
<p>Thank you for the understanding! </p>
https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32130#post-id-32130I updated the question. When I run "sudo sage" I start Sage and can run commands. I tried basic arithmetic and it works fine.Sat, 09 Jan 2016 12:55:26 -0600https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32130#post-id-32130Comment by vdelecroix for <p>I was following a <a href="http://doc.sagemath.org/html/en/tutorial/programming.html#standalone-python-sage-scripts">tutorial</a>. I tried to run this code, but got sage is not recognized error. I am using Ubuntu (Mint). I added the sage root to my path. Now when I print $PATH in the terminal there is the sage path indeed. However, my script still does not recognize the Sage package. What may I be doing wrong? If any additional info is needed I will add it.</p>
<p>Here is the output of running $PATH:</p>
<blockquote>
<p>bash:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/goran/Sage/sage-6.9-x86_64-Linux/sage</p>
</blockquote>
<p>I am wondering what my header should look like (of the Python script). In the given example it is </p>
<pre><code>#!/usr/bin/env sage.
</code></pre>
<p>I have been trying to adapt some other answers but none worked for me. What I want to achieve is calling Sage in Python code, because I have already written a couple of Python modules, and no I just need to call some Sage functions. </p>
<p>Thank you for the understanding! </p>
https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32129#post-id-32129What happens if you just type "sage" from the command line? Do you get Sage starting?Sat, 09 Jan 2016 12:48:45 -0600https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32129#post-id-32129Comment by vdelecroix for <p>I was following a <a href="http://doc.sagemath.org/html/en/tutorial/programming.html#standalone-python-sage-scripts">tutorial</a>. I tried to run this code, but got sage is not recognized error. I am using Ubuntu (Mint). I added the sage root to my path. Now when I print $PATH in the terminal there is the sage path indeed. However, my script still does not recognize the Sage package. What may I be doing wrong? If any additional info is needed I will add it.</p>
<p>Here is the output of running $PATH:</p>
<blockquote>
<p>bash:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/goran/Sage/sage-6.9-x86_64-Linux/sage</p>
</blockquote>
<p>I am wondering what my header should look like (of the Python script). In the given example it is </p>
<pre><code>#!/usr/bin/env sage.
</code></pre>
<p>I have been trying to adapt some other answers but none worked for me. What I want to achieve is calling Sage in Python code, because I have already written a couple of Python modules, and no I just need to call some Sage functions. </p>
<p>Thank you for the understanding! </p>
https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32128#post-id-32128I upvoted your question and you might be able to post the link. If it is so, you can update your question.Sat, 09 Jan 2016 12:47:35 -0600https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32128#post-id-32128Answer by vdelecroix for <p>I was following a <a href="http://doc.sagemath.org/html/en/tutorial/programming.html#standalone-python-sage-scripts">tutorial</a>. I tried to run this code, but got sage is not recognized error. I am using Ubuntu (Mint). I added the sage root to my path. Now when I print $PATH in the terminal there is the sage path indeed. However, my script still does not recognize the Sage package. What may I be doing wrong? If any additional info is needed I will add it.</p>
<p>Here is the output of running $PATH:</p>
<blockquote>
<p>bash:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/goran/Sage/sage-6.9-x86_64-Linux/sage</p>
</blockquote>
<p>I am wondering what my header should look like (of the Python script). In the given example it is </p>
<pre><code>#!/usr/bin/env sage.
</code></pre>
<p>I have been trying to adapt some other answers but none worked for me. What I want to achieve is calling Sage in Python code, because I have already written a couple of Python modules, and no I just need to call some Sage functions. </p>
<p>Thank you for the understanding! </p>
https://ask.sagemath.org/question/32126/running-sage-inside-python/?answer=32132#post-id-32132You can not use the native python of your Ubuntu and call Sage functions. The native Python does not know anything about Sage functions. One possibility might be to use the Python from Sage. Just write a file "my_script" (no extension is needed) with
#!/complete/path/to/sage -python
import sage.all # this is mandatory to initialize Sage
from sage.rings.arith import factor
print factor(18)
Then from the console do
$ ./my_script
2 * 3^2
Note that the magic of the #! just tells bash to use the Python from Sage. It has nothing to do with native Python versus Sage's Python.
extra remark: It seems not possible to use /usr/bin/env in conjunction with options...Sat, 09 Jan 2016 13:11:02 -0600https://ask.sagemath.org/question/32126/running-sage-inside-python/?answer=32132#post-id-32132Comment by A.Alharbi for <p>You can not use the native python of your Ubuntu and call Sage functions. The native Python does not know anything about Sage functions. One possibility might be to use the Python from Sage. Just write a file "my_script" (no extension is needed) with</p>
<pre><code>#!/complete/path/to/sage -python
import sage.all # this is mandatory to initialize Sage
from sage.rings.arith import factor
print factor(18)
</code></pre>
<p>Then from the console do</p>
<pre><code>$ ./my_script
2 * 3^2
</code></pre>
<p>Note that the magic of the #! just tells bash to use the Python from Sage. It has nothing to do with native Python versus Sage's Python.</p>
<p>extra remark: It seems not possible to use /usr/bin/env in conjunction with options...</p>
https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32144#post-id-32144@gilieve I'd recommend Jupyter.Sun, 10 Jan 2016 08:53:04 -0600https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32144#post-id-32144Comment by giliev for <p>You can not use the native python of your Ubuntu and call Sage functions. The native Python does not know anything about Sage functions. One possibility might be to use the Python from Sage. Just write a file "my_script" (no extension is needed) with</p>
<pre><code>#!/complete/path/to/sage -python
import sage.all # this is mandatory to initialize Sage
from sage.rings.arith import factor
print factor(18)
</code></pre>
<p>Then from the console do</p>
<pre><code>$ ./my_script
2 * 3^2
</code></pre>
<p>Note that the magic of the #! just tells bash to use the Python from Sage. It has nothing to do with native Python versus Sage's Python.</p>
<p>extra remark: It seems not possible to use /usr/bin/env in conjunction with options...</p>
https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32140#post-id-32140OK, I am trying now to run it from PyCharm and to configure PyCharm to accept the Sage packets and not give me warnings. If you have any suggestion for good IDE they are very welcome :) However, this is out of the scope of this site and your answer explained me several misunderstandings. Thanks! :)Sat, 09 Jan 2016 15:44:17 -0600https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32140#post-id-32140Comment by vdelecroix for <p>You can not use the native python of your Ubuntu and call Sage functions. The native Python does not know anything about Sage functions. One possibility might be to use the Python from Sage. Just write a file "my_script" (no extension is needed) with</p>
<pre><code>#!/complete/path/to/sage -python
import sage.all # this is mandatory to initialize Sage
from sage.rings.arith import factor
print factor(18)
</code></pre>
<p>Then from the console do</p>
<pre><code>$ ./my_script
2 * 3^2
</code></pre>
<p>Note that the magic of the #! just tells bash to use the Python from Sage. It has nothing to do with native Python versus Sage's Python.</p>
<p>extra remark: It seems not possible to use /usr/bin/env in conjunction with options...</p>
https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32138#post-id-32138It is not an issue with Python version. Sage uses its own copy of python. You can get its version with
$ sage -python --version
Python 2.7.10
Note that you are not **compiling** anything here. The command "sage file_name.sage" just **runs** the code in "file_name.sage".
The header of the file is just here to get rid of "sage" in "sage file_name.sage". I updated my answer to make it clearer as apparently you are confusing many things.Sat, 09 Jan 2016 13:36:15 -0600https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32138#post-id-32138Comment by giliev for <p>You can not use the native python of your Ubuntu and call Sage functions. The native Python does not know anything about Sage functions. One possibility might be to use the Python from Sage. Just write a file "my_script" (no extension is needed) with</p>
<pre><code>#!/complete/path/to/sage -python
import sage.all # this is mandatory to initialize Sage
from sage.rings.arith import factor
print factor(18)
</code></pre>
<p>Then from the console do</p>
<pre><code>$ ./my_script
2 * 3^2
</code></pre>
<p>Note that the magic of the #! just tells bash to use the Python from Sage. It has nothing to do with native Python versus Sage's Python.</p>
<p>extra remark: It seems not possible to use /usr/bin/env in conjunction with options...</p>
https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32137#post-id-32137My Ubuntu has Python 2.7, is that the issue? I saw somewhere that Sage uses Python 2.6. I have tried your suggestion, by writing .sage file and compiling it as sage "sage file_name.sage" and it works. But then I wanted to rename all my existing .py files into .sage files and I got issues with importing packages I have written. And if I cannot import my packages I do not see how can I separate my logic in separate files. I would like to keep writing native python code and only call Sage occasionally. I am working in PyCharm which gives me auto complete suggestions, which is of great help for writing Python code, but not Sage code.
Thank you for the suggestion!Sat, 09 Jan 2016 13:30:39 -0600https://ask.sagemath.org/question/32126/running-sage-inside-python/?comment=32137#post-id-32137