First time here? Check out the FAQ!

Ask Your Question
1

Import just one function from .sage file

asked 7 years ago

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?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 7 years ago

tmonteil gravatar image

updated 7 years ago

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.

Preview: (hide)
link

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 ( 7 years ago )
2

answered 7 years ago

vdelecroix gravatar image

updated 7 years ago

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
Preview: (hide)
link

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: 7 years ago

Seen: 2,676 times

Last updated: Dec 13 '17