Ask Your Question
1

Import just one function from .sage file

asked 2017-12-02 07:21:59 +0100

jaebond gravatar image

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?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2017-12-02 09:55:36 +0100

tmonteil gravatar image

updated 2017-12-02 17:19:23 +0100

vdelecroix gravatar image

Not 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.

edit flag offensive delete link more

Comments

This 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.

vdelecroix gravatar imagevdelecroix ( 2017-12-02 17:22:22 +0100 )edit
2

answered 2017-12-02 17:27:41 +0100

vdelecroix gravatar image

updated 2017-12-13 00:02:14 +0100

As 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_file
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2017-12-02 07:21:59 +0100

Seen: 2,501 times

Last updated: Dec 13 '17