Spyder SymPy Wont Print Symbolic Math
Asked Answered
M

2

7

I setup Anaconda 2.0.0 (Win 64). It has SymPy 0.7.5.

I configured Spyder (2.3.0rc that came with Anaconda) to use symbolic math:

Tools > Preferences > iPython console > Advanced Settings > Symbolic Mathematics

I create a new project and a new file:

# -*- coding: utf-8 -*-
from sympy import *
init_printing(use_unicode=False, wrap_line=False, no_global=True)

x = Symbol('x')
integrate(x, x)

print("Completed.")

When I run this (Python or iPython console) it does not print the integral -- it only prints Completed.

But what is weird is that while in the console that just did the run if I then re-type:

integrate(x, x)

It does print the integral.

So running from a file never prints any symbolic math but typing in the console manually does?

Can anyone help with this issue -- maybe it some sort of configuration?

Thank you!

Mulish answered 2/6, 2014 at 18:0 Comment(1)
How exactly do you run the file in IPython? Using %run?Fat
F
14

Running a script is not the same as executing code in IPython. When you run the code in a cell or prompt in IPython, it captures the output of the last command and displays it to you. When you run a script, the script is just run, and the only thing that is displayed is what is printed to the screen.

I don't think there is a way to send the IPython display object (which would be needed to get pretty latex output) from a script, but I may be misunderstanding how spyder executes the code in IPython, or missing some hooks that it has. You can try

from IPython.display import display
display(integrate(x, x))
Fat answered 2/6, 2014 at 20:21 Comment(1)
Oh sorry. I forgot that IPython.display is the module, not the function.Fat
B
4

It is because integrate doesn't print automatically, it just returns the output. You will have to pass it to print function to get the output. Try using following code:

# -*- coding: utf-8 -*-
from sympy import *
init_printing(use_unicode=False, wrap_line=False, no_global=True)
x = Symbol('x')
print(integrate(x, x))
print("Completed.")

In Python console(or IPython console) returned statements are automatically printed.

Update: Use pprint for a nice formatted output.

Blanchblancha answered 2/6, 2014 at 19:17 Comment(4)
When I use print I get regular text printing in Spyder's iPython console. If I type in the console: integrate(x,x) then I get nice rich text. Can I force my code to output nice rich text when it runs too?Mulish
It's odd that it works without print in an iPython Web Notebook. If I could get Spyder iPython console to behave like a web notebook that would be great! (actually in web notebook it does not work if I have a print statement like print("Completed') but it does work if there are no competing print statements!Mulish
Yeah you can do so. You'll have to use pprint for that. Import it from sympy.printing.Blanchblancha
pprint does do some formatting but still only with basic text -- it still does not give the nice rich text that you get if you type the calls manually into the iPython console. So if I run with pprint I get a text output and then if I go to that console and type integrate(x,x) I then get the nice rich text.Mulish

© 2022 - 2024 — McMap. All rights reserved.