Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 print function does not work properly in Python consoles #3437

Closed
Skottfelt opened this issue Sep 20, 2016 · 9 comments
Closed

Python 3 print function does not work properly in Python consoles #3437

Skottfelt opened this issue Sep 20, 2016 · 9 comments

Comments

@Skottfelt
Copy link

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)

@goanpeca
Copy link
Member

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

@Skottfelt
Copy link
Author

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

@goanpeca
Copy link
Member

Hmm got it, we will look into it

@Skottfelt
Copy link
Author

Thanks

@ccordoba12 ccordoba12 changed the title python3 print function does not work properly in spyder console Python 3 print function does not work properly in Python consoles Sep 20, 2016
@ccordoba12 ccordoba12 added this to the wishlist milestone Sep 20, 2016
@ccordoba12
Copy link
Member

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 :-)

@ccordoba12 ccordoba12 removed this from the wishlist milestone Jan 9, 2017
@ccordoba12
Copy link
Member

Closing as Won't fix.

@spyder-ide spyder-ide deleted a comment Oct 9, 2017
@jnettels
Copy link

jnettels commented Jan 5, 2018

  • 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

@jnsebgosselin
Copy link
Member

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

@jnsebgosselin
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants