question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Python 3 print function does not work properly in Python consoles

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
jnsebgosselincommented, Jan 8, 2018

@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.

1reaction
jnsebgosselincommented, Jan 5, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does this Python code not print anything - Stack Overflow
I would assume it's due to buffering. Try adding flush=True as one of the optional parameter to print .
Read more >
Your Guide to the Python print() Function
In this step-by-step tutorial, you'll learn about the print() function in Python and discover some of its lesser-known features.
Read more >
not printing in console... - Codecademy
A common thing to do is to store the returned value somewhere, so x = trip_cost("Los Angeles", 5, 600) would run the function...
Read more >
__main__ — Top-level code environment — Python 3.11 ...
Code within this block won't run unless the module is executed in the ... there is some # valuable and reusable logic inside...
Read more >
Print Function and Strings - Python Programming Tutorials
If something isn't acting right, you can use the print function to print out what is happening in the program. Many times, you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found