1

The code in question:

print('s')
y=1/0

In which I understand the error perfectly (it's just division by zero), causes, when ran (while on PyCharm, by pressing shift+f10, the normal way) either the following output:

C:\Users\myname\PycharmProjects\test1\.venv\Scripts\python.exe C:\Users\myname\PycharmProjects\test1\Functions\error.py 
Traceback (most recent call last):
  File "C:\Users\myname\PycharmProjects\test1\Functions\error.py", line 2, in <module>
    y=1/0
      ~^~
ZeroDivisionError: division by zero
s

Process finished with exit code 1

Or this one (and now note that the printed 's' appears before the traceback instead of after:

C:\Users\myname\PycharmProjects\test1\.venv\Scripts\python.exe C:\Users\myname\PycharmProjects\test1\Functions\error.py 
s
Traceback (most recent call last):
  File "C:\Users\myname\PycharmProjects\test1\Functions\error.py", line 2, in <module>
    y=1/0
      ~^~
ZeroDivisionError: division by zero

Process finished with exit code 1

This is a bug. I have had this issue in programs with many more statements and sometimes the traceback is hidden before them.

I tried searching similar questions, but they don't focus on if it's important or not the order shown of the output if there is a traceback, and don't have so simple errors, and they are older.

1
  • There's a 4-years-old bug report for precisely this bug, though a much older one was marked as "fixed". Having said that, I don't think this question have anything new compared to the linked one, since the error type is most probably irrelevant and the order is the main problem in not just this one but both. If you think an old question needs some attention, we have bounties for that.
    – InSync
    Commented Jan 17 at 3:40

1 Answer 1

2

Some cursory research leads me to believe this is indeed a bug in PyCharm (and is seemingly pervasive throughout the rest of JetBrains' IDEs) relating to how stdout and stderr are printed.

GitHub user @wolph, owner of the python-progressbar project, stated it more eloquently than I can:

The bug within PyCharm is with the timing between stderr and stdout which appears fully random causing interleaved results.

There have been multiple reports of this bugged behavior in JetBrains' publicly-facing bug tracker made over the past 12 years. Some of those which are (seemingly) related are still marked "Open":

Even so, JetBrains employee Sergey Simonchik provided a potential workaround in 2019:

Please note that proper ordering between stdout and stderr is still not guaranteed by default, but there is a way to enforce it with run.processes.with.redirectedErrorStream registry key. To enable it, do the following:

  • "Help | Find Action..." on the main menu; find "Registry..." item and open it;
  • Enable run.processes.with.redirectedErrorStream key.

Enabling it will merge stdout and stderr streams of processes spawned by IDE. This will ensure proper ordering of messages from stdout/stderr.

[...]

However, as a price, merging stdout and stderr will make them indistinguishable: error output will be printed as normal text. Use ANSI coloring to highlight error output differently.

Please note that the registry key is supported for requested run/debug configurations only. If it is not supported for your run/debug configuration, please submit a request in issue tracker.


Given the bug's prevalence after >=12 years of reports, it doesn't seem like this is a very high priority for JetBrains to fix. If this functionality is important for you to get right in your project (and the above workaround doesn't work in your case, for whatever reason), I'd personally recommend evaluating another IDE. There is a litany to choose from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.