Ask Your Question
0

Import just one function from .sage file

asked 2017-12-02 00:21:59 -0600

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
1

answered 2017-12-02 02:55:36 -0600

tmonteil gravatar image

updated 2017-12-02 10:19:23 -0600

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 10:22:22 -0600 )edit
2

answered 2017-12-02 10:27:41 -0600

updated 2017-12-12 17:02:14 -0600

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

1 follower

Stats

Asked: 2017-12-02 00:21:59 -0600

Seen: 39 times

Last updated: Dec 12