Ask Your Question
0

[RESOLVED]Use sage objects in python module files

asked 2017-03-07 13:34:02 +0100

Hyorvenn gravatar image

updated 2017-03-11 16:24:45 +0100

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 21:13:03 +0100

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 21:21:08 +0100 )edit

Indeed! Thanks for the correction.

eric_g gravatar imageeric_g ( 2017-03-07 21:29:48 +0100 )edit

A pointer to the doc? Thanks!

maaaaaaartin gravatar imagemaaaaaaartin ( 2017-03-09 12:19:38 +0100 )edit
1
mforets gravatar imagemforets ( 2017-03-11 20:53:12 +0100 )edit
0

answered 2017-03-09 12:19:09 +0100

maaaaaaartin gravatar image

updated 2017-03-11 19:14:34 +0100

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 16:22:28 +0100

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 13:34:02 +0100

Seen: 1,054 times

Last updated: Mar 11 '17