Extension: Python workspace settings:
setting.json
"python.pythonPath"="~/.virtualenvs/[virtualenv name]/bin/python"
There is no problem if it is executed in the terminal of mac, but if it is executed in Run Python File in Terminal
on the Visual Studio side, if Japanese comments or something is entered, UnicodeEncodeError ...
Of course, it can be executed in the terminal on Mac, so I understood that it was a problem on the Python execution terminal side of Visual Studio.
For the time being,
hogehoge.py
import sys
print(sys.stdout.encoding)
When I executed, "utf-8" is displayed in the terminal on the Mac side, and "US-ASCII" is displayed in Visual Studio. What the hell is that?
So, if you add the standard output encoding change to the file that caused the error earlier, it can be executed properly.
However, it is troublesome to fill in every time, so I decided to create a sitecustomize.py file. It's virtualenv anyway, so you can delete it when you don't need it anyway, so create the following file and place it under site-packages specified in virtualenv.
sitecustomize.py
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
It is said that Japanese can be used not only on the terminal but also on the Visual Studio Code terminal.
I think that it is the part that is originally set in the part set by the extension function of Python execution, so I will update it when I find it. (It may be powerful.)
Recommended Posts