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.Sat, 02 Dec 2017 17:27:41 +0100Import just one function from .sage filehttps://ask.sagemath.org/question/39906/import-just-one-function-from-sage-file/I have a `.sage` file that has a single function I want to import. If it were a `.py` file, I would obviously use `from temp import myfun`, but as far as I know, `.sage` files have to be imported with either `load('temp.sage')` or `attach('temp.sage')` (or preparsed, but this gets tiresome). Is there a way to import just one function from a `.sage` file?Sat, 02 Dec 2017 07:21:59 +0100https://ask.sagemath.org/question/39906/import-just-one-function-from-sage-file/Answer by tmonteil for <p>I have a <code>.sage</code> file that has a single function I want to import. If it were a <code>.py</code> file, I would obviously use <code>from temp import myfun</code>, but as far as I know, <code>.sage</code> files have to be imported with either <code>load('temp.sage')</code> or <code>attach('temp.sage')</code> (or preparsed, but this gets tiresome). Is there a way to import just one function from a <code>.sage</code> file?</p>
https://ask.sagemath.org/question/39906/import-just-one-function-from-sage-file/?answer=39907#post-id-39907Not that I know. However, you should notice that the difference between `.sage` and `.py` files is a thin preparsing difference to ease daily use (like `^` for powers instead of `**`). Also, Sage source is written with `.py` files, not `.sage` file. So, if you plan fo write a module, let me just suggest to transform your `.sage` file into a `.py` module. The `preparse` function is a tool to look at.Sat, 02 Dec 2017 09:55:36 +0100https://ask.sagemath.org/question/39906/import-just-one-function-from-sage-file/?answer=39907#post-id-39907Comment by vdelecroix for <p>Not that I know. However, you should notice that the difference between <code>.sage</code> and <code>.py</code> files is a thin preparsing difference to ease daily use (like <code>^</code> for powers instead of <code>**</code>). Also, Sage source is written with <code>.py</code> files, not <code>.sage</code> file. So, if you plan fo write a module, let me just suggest to transform your <code>.sage</code> file into a <code>.py</code> module. The <code>preparse</code> function is a tool to look at.</p>
https://ask.sagemath.org/question/39906/import-just-one-function-from-sage-file/?comment=39916#post-id-39916This is not **the** difference. A `.py` file is considered by Python as a valid module (that you can import from). But a `.sage` file is not. And this is precisely the OP problem.Sat, 02 Dec 2017 17:22:22 +0100https://ask.sagemath.org/question/39906/import-just-one-function-from-sage-file/?comment=39916#post-id-39916Answer by vdelecroix for <p>I have a <code>.sage</code> file that has a single function I want to import. If it were a <code>.py</code> file, I would obviously use <code>from temp import myfun</code>, but as far as I know, <code>.sage</code> files have to be imported with either <code>load('temp.sage')</code> or <code>attach('temp.sage')</code> (or preparsed, but this gets tiresome). Is there a way to import just one function from a <code>.sage</code> file?</p>
https://ask.sagemath.org/question/39906/import-just-one-function-from-sage-file/?answer=39918#post-id-39918As Thierry suggested you would better use `.py` files. A quick conversion can be done from the console with
$ sage -preparse my_file.sage
Note that the produced file will be `my_file.sage.py` which makes it unusable from Sage as a module! So first change its name
$ mv my_file.sage.py my_file.py
And then you can use it from a Sage console
sage: import my_fileSat, 02 Dec 2017 17:27:41 +0100https://ask.sagemath.org/question/39906/import-just-one-function-from-sage-file/?answer=39918#post-id-39918