VS Code Python Debugger Throws an Error with Print Statements

So, whenever my friends try to debug any Python script in VS Code and the script has a print() or input() statement, it crashes and throws me an “AttributeError” saying 'NoneType' object has no attribute 'write'. I’ve been looking around for answers, but I haven’t been able to find anything useful on Google. I’ve attached a link to a screenshot of the error, and here’s my configuration file below. It seems to happen when I use the integratedTerminal for debugging. Based on what I’ve learned, it could be that the integrated terminal isn’t handling the print or input statements properly, which is causing this crash. My launch.json file is configured to use both the integratedTerminal and externalTerminal, but I’m thinking of sticking with the externalTerminal since it might avoid this error. Any suggestions on how to get around this would be helpful!

Python Code (with Errors):

code{ 
// Use IntelliSense para saber los atributos posibles.
// Mantenga el puntero para ver las descripciones de los existentes atributos
// Para más información, visite: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost"
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true
},
{
"name": "Python: Current File (External Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
}
]
}

Explanation:

When debugging Python scripts in Visual Studio Code (VS Code), especially those that use print() or input() statements, you might encounter an error. This error commonly manifests as an AttributeError, with the message 'NoneType' object has no attribute 'write'. This issue often stems from the use of the integrated terminal within VS Code, which may not handle I/O operations properly for certain configurations.

In the example configuration file above, the script specifies the use of the integrated terminal for debugging Python files. However, if your script relies heavily on input() or print() functions, this configuration might fail.

Common Issue:

The error 'NoneType' object has no attribute 'write' indicates that the standard input/output (I/O) stream might not be properly set up in the integrated terminal, which leads to crashes when trying to print or get input from the user. This error can also occur if the environment setup for your terminal isn’t fully supporting the I/O operations expected by Python.

To fix this issue, switch from using the integrated terminal to an external terminal. The external terminal handles I/O more reliably in these cases. Below is the corrected configuration file to resolve this issue.

Correct Code:

code{ 
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (External Terminal)",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"program": "${file}",
"console": "externalTerminal" // Changed to externalTerminal to resolve I/O issues
},
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost"
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "externalTerminal", // Also using externalTerminal for Django
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true,
"console": "externalTerminal" // Changed to externalTerminal for Flask
}
]
}

Explanation of Changes:

  1. Switch to External Terminal:
    In the corrected configuration, I’ve changed the console attribute to externalTerminal. This allows you to run your Python scripts in an external terminal, which is much more reliable when handling print() and input() statements compared to the integrated terminal.
  2. External Terminal for Django and Flask:
    For both Django and Flask applications, I also made sure to set console to externalTerminal. These frameworks may also encounter issues with I/O operations, especially when using debugging features, and running them in an external terminal helps avoid these problems.

Final Thoughts:

Switching to an external terminal is the most straightforward solution for debugging Python scripts with I/O operations like print() or input(). It resolves the NoneType errors and ensures that the Python script can run and handle user input/output correctly. If you continue to encounter issues, it’s always a good idea to double-check that the correct Python interpreter is selected and that your environment is properly set up within VS Code.

Related blog posts