[PYTHON] Test discovery fails when using tensorflow in vscode + pytest environment

What is this article

While moving the Python development environment to VSCode, I tried various things to run pytest in the VSCode environment, but for some reason the discovery failed and I got stuck for a while.

background

I am creating a new Python project in the VSCode environment connected by Remote Development. I created the Python environment with poetry and introduced pytest for testing. I confirmed from CUI that pytest can run poetry run pytest without any problem, and when I tried to run the test from VSCode, the test discovery failed.

Cause

When doing test discovery with pytest from VSCode, for some reason it was treated as a failure if there was standard output. It has been pointed out from the following issues that it will fail if there is any standard output. https://github.com/microsoft/vscode-python/issues/6594 https://github.com/microsoft/vscode-python/issues/7574

If you are using TensorFlow, just doing ʻimport tensorflow as tf` will give you various warnings, which seems to be the cause.

solution

It is as in the above issue. To control the output of TensorFlow, put a file like the following in your project with the name .env.

.env


TF_CPP_MIN_LOG_LEVEL='2'

An environment variable for controlling the TF log output. By setting 2, WARNING will not appear. If you set it to 3, no error will occur.

If we can prevent other types of standard output in some way, we can take measures.

that's all. It took me some time to realize that the cause was standard output, so if it helps ...

Recommended Posts

Test discovery fails when using tensorflow in vscode + pytest environment
Environment variables when using Tkinter
A story that stumbled when using pip in a proxy environment
Use "% tensorflow_version 2.x" when using TPU with Tensorflow 2.1.0 in Colaboratory
Debug settings in virtual environment when using Pipenv with VS Code
Using TensorFlow in the cloud integrated development environment Cloud9 ~ Basics of usage ~
Precautions when using pit in Python
When pip fails under proxy environment
install tensorflow in anaconda + python3.5 environment
When using regular expressions in Python
Notify using Notification Center when the execution environment is macOS in Python
code-server Local environment (3) Try using VSCode Plugin
Use tensorflow in an environment without root
Using venv in Windows + Docker environment [Python]
Using TensorFlow in Cloud9 Integrated Development Environment-GetStarted-
CSS environment created in 10 minutes using Django
Precautions when using for statements in pandas
Building scikit-learn in Windows 10 environment using Pycharm
Error, warning when using TensorFlow on Mac
What to do when SSL error occurs in pip in Windows10, miniconda, VScode environment
Notify using Notification Center when the execution environment is macOS in Python