It may happen that you execute a Python script and nothing happenes, i.e. it seems that it “hangs” without showing any output.
Even if you explicitly include some print()
statements to debug the issue, it may still not print anything to a terminal.
One of the reasons of why your Python script does not show any output is because it buffers stdout
and stderr
steams instead of printing them.
This also can happen if you execute the Python script from a CI/CD pipeline (e.g. using Jenkins, Gitlab-CI, TeamCity, etc.) or if you run it using a Dockerfile
.
This short note shows how to solve the issue when the Python script “hangs” and doesn’t show any output.
Cool Tip: How to check the version of a Python package! Read More →
Python Script Not Showing Output
To force the stdout
and stderr
streams to send the output of a Python script straight to a terminal (or a log file), you can set the PYTHONUNBUFFERED
environment variable (to any value different from 0
) or execute the python
command with the -u
option.
The PYTHONUNBUFFERED
environment variable can be set as follows:
# Shell $ export PYTHONUNBUFFERED=true $ python script.py # Dockerfile ENV PYTHONUNBUFFERED=true CMD [ "python", "script.py" ]
Or you can simply run the python
command with the -u
option:
# Shell $ python -u script.py # Dockerfile CMD [ "python", "-u", "script.py" ]
After using any of the methods above, your Python script should start printing the output.
Cool Tip: How to list all the locally installed Python modules and find the paths to their source files! Read More →
It is not working did you have other to solution on this
I tried that, but I am still not getting an output. I have tried it on PyCharm and IDLE.
not working
Not working too