A note I was addicted to when running Python with Visual Studio Code

environment

  1. I'm switching libraries using Python3 series, virtualenv on Mac
  2. Settings on the Visual Studio side

Extension: Python workspace settings:

setting.json


	"python.pythonPath"="~/.virtualenvs/[virtualenv name]/bin/python"

a problem occured···

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.

Solution?

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

A note I was addicted to when running Python with Visual Studio Code
A note I was addicted to when creating a table with SQLAlchemy
I was addicted to creating a Python venv environment with VS Code
A story that I was addicted to when I made SFTP communication with python
Try to create a python environment with Visual Studio Code & WSL
A note I was addicted to when making a beep on Linux
A story I was addicted to when inserting from Python to a PostgreSQL table
I was addicted to trying Cython with PyCharm, so make a note
I was addicted to scraping with Selenium (+ Python) in 2020
What I was addicted to when using Python tornado
Three things I was addicted to when using Python and MySQL with Docker
[python] A note when trying to use numpy with Cython
What I was addicted to when migrating Processing users to Python
What I was addicted to when introducing ALE to Vim for Python
What I was addicted to with json.dumps in Python base64 encoding
What I was addicted to when dealing with huge files in a Linux 32bit environment
What I was addicted to Python autorun
Try it when Visual Studio Code can't load a Python module
When I tried to scrape using requests in python, I was addicted to SSLError, so a workaround memo
Note that I was addicted to accessing the DB with Python's mysql.connector using a web application.
How to hide the command prompt when running python in visual studio 2015
I was addicted to running tensorflow on GPU with NVIDIA driver 440 + CUDA 10.2
A story I was addicted to trying to get a video url with tweepy
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
I installed the library with Visual Studio Code, but Unable to import
Build Python development environment with Visual Studio Code
I want to make a game with Python
I get a UnicodeDecodeError when running with mod_wsgi
I want to write to a file with Python
I get an error when I put a Python plugin in Visual Studio Code under the pyenv environment
What I was addicted to when creating a web application in a windows environment
When I tried to create a virtual environment with Python, it didn't work
Summary of points I was addicted to running Selenium on AWS Lambda (python)
I customized it with Visual Studio Code (mainly for python), so I will summarize it
I set up TensowFlow and was addicted to it, so make a note
Preparing to use Tensorflow (Anaconda) with Visual Studio Code
Python Ver. To introduce WebPay with a little code.
I tried to draw a route map with Python
I want to work with a robot in python.
Things to note when initializing a list in Python
From buying a computer to running a program with python
I tried to automatically generate a password with Python3
Python development environment with Windows + Anaconda3 + Visual Studio Code
A story that I was addicted to at np.where
A note on speeding up Python code with Numba
Python development environment with Windows + Python + PipEnv + Visual Studio Code
Japanese output when dealing with python in visual studio
I want to run a quantum computer with Python
I was soberly addicted to calling awscli from a Python 2.7 script registered in crontab
[Python] I want to use only index when looping a list with a for statement
I got stuck when trying to specify a relative path with relative_to () in python
[IOS] GIF animation with Pythonista3. I was addicted to it.
I made a package to filter time series with python
I wrote a program quickly to study DI with Python ①
[Fixed] I was addicted to alphanumeric judgment of Python strings
When I tried to install PIL and matplotlib in a virtualenv environment, I was addicted to it.
When creating a pipenv environment, I got addicted to "Value Error: Not a valid python path"
I was surprised to receive a nice review when I wrote Python to CheckIO and its explanation
The story I was addicted to when I specified nil as a function argument in Go
[Go language] Be careful when creating a server with mux + cors + alice. Especially about what I was addicted to around CORS.
It was a painful memory when I was told TypeError: must be type, not class obj when trying to inherit with Python.