Possible bug in encoding stdout [eventually bug in pyenv-win]

I am on windows 10.

When I set powershell console to be in utf8,

python -X  utf8 -c "print('hh')"

print nothing

But

python -X  utf8 -c "import sys;  sys.stdout.flush();print('hh')"

Does print…

python -X utf8 -c "print('hh'); print('hfgh');"

prints as well.

Also if I add sleep (1 print).

It seems like a bug to me( and slows my progress)..

Tried on both python 3.11.5 and 3.13.0 (free-threaded) .

1 Like

It works fine for me in PowerShell with Python 3.13.0 (hh is displayed as expected).

When I set powershell console to be in utf8

Can you elaborate? How do you do that? (I don’t know PowerShell).

For what it’s worth, UTF-8 mode shouldn’t affect a normal print in a normal console. It will only affect streams redirected to files (in this context - it affects some other things, but not sys.stdout in a console).

I am not sure what the “unexpected at the time” means.

Yeh, possibly it wasn’t necessary “setting it to etf8” (this is what it means)

[System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[System.Console]::InputEncoding = [System.Text.Encoding]::UTF8

I tried changing input to output etc. It doesn’t happen from cmd.

$OutputEncoding should show you the current information about encoding, and chcp 65001 should set output to utf8 if it isn’t already.

1 Like

It looks like you have some problem with Powershell parsing into the string literal and thinking that the semicolon is for it, rather than something it should just pass along to Python.

What does Get-Command python show? It seems like you aren’t invoking a normal executable. It might also be a bug in that version of PowerShell, so if there are any updates, it’s worth taking them just in case.

1 Like

The “unexpected at this time” message is typically an error from cmd.exe. Could the python command actually be a .bat file that the user has somehow managed to install, rather than the standard Python executable?

2 Likes

Ah yes, you’re right (it’s been a while since I ran into it :wink: ). It’s probably a batch file that is processing some arguments, and so the quotes are being stripped off and not all of the arguments are being passed.

Get-Command python will show it (possibly (Get-Command python).Source if the path isn’t fully visible).

Yes, you are right. That is pywin-env. When I run just the executable it doesn’t do it. Probably pyenv bug. I didn’t think about this possiblity ( I am so used to that). Thanks everyone!

3 Likes