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!

Matt M.
  • 812
  • 3
  • 16
  • 24

2 Answers2

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))
asmeurer
  • 86,894
  • 26
  • 169
  • 240
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.

Sudhanshu Mishra
  • 2,024
  • 1
  • 22
  • 40
  • 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? – Matt M. Jun 02 '14 at 19:35
  • 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! – Matt M. Jun 02 '14 at 19:37
  • Yeah you can do so. You'll have to use `pprint` for that. Import it from `sympy.printing`. – Sudhanshu Mishra Jun 02 '14 at 20:06
  • 1
    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. – Matt M. Jun 02 '14 at 21:08