Spyder: Python 3 print function does not work properly in Python consoles

Created on 20 Sep 2016  路  9Comments  路  Source: spyder-ide/spyder

Description of your problem

I am running python 2.7, but uses the print function from python3 in order to use the overwrite functionality. However this does not work porperly in the spyder console.

What steps will reproduce the problem?
An example:

from __future__ import print_function
import time 
import sys

for i in range(10):
    print(i,end='\r')
    time.sleep(0.5)
    sys.stdout.flush()

What is the expected output? What do you see instead?
This should not change the line, but the numbers should simply be overwritten. However in the spyder console, a linebreak seems to be inserted, such that the output is:
0
1
2
3
4
5
etc.

In the IPython console in Spyder the output is
0123456789

Please provide any additional information below
I am using the latest spyder3 update, but spyder2 had the same problem

Versions and main components

  • Spyder Version: Spyder 3.0.0.dev0 beta6, Qt 5.6.0, PyQt5 5.6
  • Python Version: Python 2.7.11 64bits
  • Operating system: Linux

    Dependencies

jedi >=0.8.1 : 0.9.0 (OK)
matplotlib >=1.0 : 1.5.1 (OK)
nbconvert >=4.0 : 4.1.0 (OK)
numpy >=1.7 : 1.10.4 (OK)
pandas >=0.13.1 : 0.18.0 (OK)
pep8 >=0.6 : 1.7.0 (OK)
psutil >=0.3 : 4.1.0 (OK)
pyflakes >=0.5.0 : 1.1.0 (OK)
pygments >=2.0 : 2.1.1 (OK)
pylint >=0.25 : 1.5.4 (OK)
qtconsole >=4.2.0: 4.2.0 (OK)
rope >=0.9.4 : 0.9.4 (OK)
sphinx >=0.6.6 : 1.3.5 (OK)
sympy >=0.7.3 : 1.0 (OK)

Python Console Bug

Most helpful comment

@AzraelDD

As a workaround, you can write instead:

import time 
import sys

for i in range(10):
    print('\r%d' % i, end='')
    time.sleep(0.5)
    sys.stdout.flush()

This works for me both in the Python, IPython, and qtconsole used by Spyder.

All 9 comments

Hi @Skottfelt why \r ? You are on Linux and Linux uses \n

Because I am using it as status output, that should overwrite itself, so I do not get a long list of unnecessary text.

Hmm got it, we will look into it

Thanks

This works correctly in our IPython consoles. Please those instead because we don't have plans to fix this for our Python consoles anytime soon :-)

Closing as Won't fix.

  • Windows 10
  • Spyder 3.2.5
  • Python 3.6.4

Hi everyone,
I am having the same problem as in the original post. When I run the code above (minus from __future__), I get the following output:
```
Python
Type "copyright", "credits" or "license" for more information.

IPython -- An enhanced Interactive Python.
In [1]: runfile('foo', wdir='bar')
0123456789

In [2]:
````
When I choose "Execute in an external system terminal" in "Run configuration per file", I get the desired output where each number is overwritten by the next one.
You wrote that "Python console" would not work, but "IPython console" should work. It seems to me like I am using the IPython console, or am I mistaken? Am I doing something wrong?

Any help is appreciated.
Regards

This is something I've stumbled upon also in the past. It seems to me this is a problem with the QtConsole not with Spyder, so maybe a new issue should be opened there?

In Spyder:

lineending_r_spyder

In a bare QtConsole:

lineending_r_qtconsole

In a notebook:

lineending_r_notebook

@AzraelDD

As a workaround, you can write instead:

import time 
import sys

for i in range(10):
    print('\r%d' % i, end='')
    time.sleep(0.5)
    sys.stdout.flush()

This works for me both in the Python, IPython, and qtconsole used by Spyder.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danieltomasz picture danieltomasz  路  3Comments

hedeqing picture hedeqing  路  3Comments

spyder-bot picture spyder-bot  路  3Comments

keith-golden picture keith-golden  路  3Comments

yogu220172 picture yogu220172  路  3Comments