Python Script Not Showing Output [SOLVED]
source link: https://www.shellhacks.com/python-script-not-showing-output-solved/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Python Script Not Showing Output [SOLVED]
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 →
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK