Ask Your Question
0

[RESOLVED]Use sage objects in python module files

asked 2017-03-07 06:34:02 -0600

Hyorvenn gravatar image

updated 2017-03-11 09:24:45 -0600

Hello,

I'm trying to figure out how to use sage functions/classes in a module .py file, from another python file. Here's the thing, I'm developing a small simulator in Python 2 intended to be use with sage for solving diff. equations, etc... So I have different .py files (interface, calcul, etc) in which have been written some code using sage functions.

Let's say I have "y = var('y')" in calcul.py and I run it from sage console ->

sage: load("calcul.py") sage:

All works properly without any failures. But, if I want to import this module in interface.py and then run the file with sage, I get this error ->

sage: load("interface.py") ---> 1 y = var('y')

NameError: name 'var' is not defined

As if it didn't recognize its own functions. Is there any command/function to use in sage to allow loading of imported files without that type of error ?

Thanks in advance for your help.

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
2

answered 2017-03-07 14:13:03 -0600

eric_g gravatar image

Indeed you have to add

from sage.calculus.var import var

in calcul.py and do the same thing for any Sage name that appears in your module.To know the precise import statement to use, run import_statements in a Sage console:

sage: import_statements(var)
from sage.calculus.var import var
sage: import_statements(SR)
from sage.symbolic.ring import SR
sage: import_statements(pi)
from sage.symbolic.constants import pi
edit flag offensive delete link more

Comments

2

And as the documentation of var explains, in a programming/library environment you're better off using y=SR.var('y'). The var function that is used on top-level has some nasty side-effects that are convenient for interactive use, but are bad in library code.

nbruin gravatar imagenbruin ( 2017-03-07 14:21:08 -0600 )edit

Indeed! Thanks for the correction.

eric_g gravatar imageeric_g ( 2017-03-07 14:29:48 -0600 )edit

A pointer to the doc? Thanks!

maaaaaaartin gravatar imagemaaaaaaartin ( 2017-03-09 05:19:38 -0600 )edit
1
mforets gravatar imagemforets ( 2017-03-11 13:53:12 -0600 )edit
0

answered 2017-03-09 05:19:09 -0600

maaaaaaartin gravatar image

updated 2017-03-11 12:14:34 -0600

My advice: put import sage.all as sage at the beginning of each python file, and then use for instance sage.var('x') in your scripts.

edit flag offensive delete link more
0

answered 2017-03-11 09:22:28 -0600

Hyorvenn gravatar image

Thanks a lot for your answers !

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

Stats

Asked: 2017-03-07 06:34:02 -0600

Seen: 100 times

Last updated: Mar 11