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, 19 Jan 2020 20:09:24 +0100Automatically attach file on starting shell?https://ask.sagemath.org/question/49607/automatically-attach-file-on-starting-shell/Is there a way to run sage from command line and have sage automatically run a command (such as attaching a file)? I do not want to put the attach command in the init.sage file, because I want sage to only attach my code when I am working on my project, not for general use.
Perhaps another way of thinking about is how can I run a sage script, and have it exit into an interactive shell? Then I would be able to run sage mycode.sage, and have the desired behavior.
Thank you.Sun, 19 Jan 2020 17:20:47 +0100https://ask.sagemath.org/question/49607/automatically-attach-file-on-starting-shell/Answer by Sébastien for <p>Is there a way to run sage from command line and have sage automatically run a command (such as attaching a file)? I do not want to put the attach command in the init.sage file, because I want sage to only attach my code when I am working on my project, not for general use.</p>
<p>Perhaps another way of thinking about is how can I run a sage script, and have it exit into an interactive shell? Then I would be able to run sage mycode.sage, and have the desired behavior.</p>
<p>Thank you.</p>
https://ask.sagemath.org/question/49607/automatically-attach-file-on-starting-shell/?answer=49608#post-id-49608In the file `~.sage/init.sage`, you may write the following code:
import os
if os.path.exists('init.sage'):
%runfile init.sage
which will automatically run the file `init.sage` if there is such a file in the current folder. Then, you can put your desired code in that file in your working directory which will get run first when you start sage.Sun, 19 Jan 2020 18:59:36 +0100https://ask.sagemath.org/question/49607/automatically-attach-file-on-starting-shell/?answer=49608#post-id-49608Comment by nbruin for <p>In the file <code>~.sage/init.sage</code>, you may write the following code:</p>
<pre><code>import os
if os.path.exists('init.sage'):
%runfile init.sage
</code></pre>
<p>which will automatically run the file <code>init.sage</code> if there is such a file in the current folder. Then, you can put your desired code in that file in your working directory which will get run first when you start sage.</p>
https://ask.sagemath.org/question/49607/automatically-attach-file-on-starting-shell/?comment=49609#post-id-49609I think you may want to use a different name in the `%runfile` command; otherwise starting up sage with `~/.sage` as current directory could lead to an unpleasant experience.Sun, 19 Jan 2020 20:09:24 +0100https://ask.sagemath.org/question/49607/automatically-attach-file-on-starting-shell/?comment=49609#post-id-49609