VS code python debugger error with print statements - python

So when ever im trying to debug any Python script on VS code an that script has a print or input statement, it crashes an throws me an "AttributeError" that says 'NoneType' object has no attribute 'write', any ideas why this happens? i cant find any information on Google about it
here is a screenshot in the error:Link to the screenshot
and also here is my configuration file:
{
// 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"
}
]
}

VSCODE working fine for me
a) Install VSCODE or upgrade to version 1.28.1
b) REFRESH Python Extension
If you face any issue like timeout etc read
https://github.com/Microsoft/vscode-python/issues/2410
very carefully
Edit settings.json of python extension
a) Disable
Terminal: Activate Environment
Activate Python Environment in Terminal created using the Extension.
b) enable
Terminal: Execute In File Dir
When executing a file in the terminal, whether to use execute in the file's directory, instead of the current open folder.
c) Remove pythonW and put python in
Python Path
Path to Python, you can use a custom version of Python by modifying this setting to include the full path.
Everything above from
https://github.com/Microsoft/vscode-python/issues/2410
Though a happy ending I can foresee a future of unstable releases
for a wonderful VSCODE and even better Python Extension

An attribute error usually means that whatever object your working with is actually none. This can happen because something happened upstream or downstream of your call.
In the case of your single print statement, the only thing I can think of is perhaps it has something do so with double quotes.. It doesn't really make sense that double quotes would cause this but who knows.
what happens when you try
print('I will crash!!!')
If that still fails then I would say that perhaps vs is trying to write to a file, configuration, log, console or something else and is running into permission issues.
EDIT
After looking closesr at your configuration files, I see you have two that start with
"name": "Python: Current File ....
So I rewrote your config file, it still includes the specific files that were named, and their configs, but I eliminated one of the current file entries and made it basic.
{
// 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 (External Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
},
{
"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
}
]
}
I have it set up to use the external console (standard windows cmd). If you want to use the vs console replace
{
"name": "Python: Current File (External Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
},
with
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"program": "${file}",
"console": "integratedTerminal"
},
Do NOT forget to save a copy of your old config file first. That way if VS freaks out from manually changing this file you can always revert back.
I am looking for the possibility that VS can't decide which terminal to output too, but at the same time you only get this while debugging so....
Now I did see a flag in the config for no debug but it was for a flask app.

Related

'No module named flask' in VSCode even when I have installed flask

I have my Flask installed in my main directory for playing with Flask projects and I don't want to change it (its name is tryFlask).
My project is in one of the subdirectories to tryFlask (main_project).
How should I config my launch.json so that it doesn't throw errors when I try to debug?
Current config which throws:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "my_app.py",
"FLASK_ENV": "tryFlask",
"FLASK_DEBUG": "1"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true,
"justMyCode": true
}
]
}
And yes I have already checked "FLASK_APP": "my_project/my_app.py". Doesn't work. Neither does reinstalling dependencies or even choosing different interpreters from venv's bin subdirectory.
If you want to use flask with vscode, following this tutorial is a good start.
According to the question in your post, if you want to specify the python interpreter when debugging, you can add the python configuration to launch.json
Simple example:
{
// 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",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
// Modify the python interpreter with flask installed on your machine
"python": "C:\\WorkSpace\\pytest10\\.venv\\Scripts\\python.exe"
// "python": "${workspaceFolder}/.venv/Scripts/python.exe"
}
]
}
For more launch.json configuration, you can check this link.

How to debug a file from current directory with Python in VS Code?

I can see that this question has been asked multiple times already however, none of the solutions seems to be working anymore, forcing me to ask for the latest fix.
Problem
I have created a workspace in VS code with the following directory structure,
work__assignments (this is my main workspace folder in vs code)
assignment__1
data
modules
tests
assignment__2
data
modules
tests
"work__assignments" is my main workspace folder in the VS code. From this main workspace, I go to an assignment folder (e.g. work__assignments > assignment__2 > modules) and work on the respective code. However, when I try to debug the code in "work__assignments > assignment__2 > modules" the debugger loads from the main workspace folder (i.e. work__assignments), then it fails because it can't find other modules in the modules folder "work__assignments > assignment__2 > modules".
I have tried the following methods so far,
Updating the launch.json file and adding the line "cwd": "${fileDirname}",
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}"
}
Updating the launch.json file and adding the line "cwd": "",
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": ""
}
Updating the launch.json file and adding the following lines,
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH": "${cwd}"
}
}
Updating the preferences
However, NONE OF THESE APPROACHES HAVE WORKED. Every time I debug the code, it launches into the workspace folder and not in the code folder. What am I doing wrong?
Note: I am running the debugger using the "Debug Python File" button at the top right corner as shown in the picture below,
Configure one of the following cwd in launch.json:
"cwd": "./assignment__2"
"cwd": "${fileDirname}"
It should be noted that you need to debug the program from the Run and Debug panel to execute the configuration in launch.json.
A simple example
Directory Structure and code
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
// "cwd": "./hello"
"cwd": "${fileDirname}"
}
]
}
Debug results
Invalid configuration in launch.json if debug is selected from play button

Fix the following: "pydevd warning: Computing repr of <something> was slow (took 0.35s)" in VS CODE

I'm working with Python with VSCode and I'm struggling with the following since this year:
pydevd warning: Computing repr of mlb (MyLibrary) was slow (took 0.35s)
Source: Debug Unit Test
This message appears for a varieties of packages, but the fix reported here:
pydevd warnings in Visual Studio Code Debug Console
is not working. I followed the relative suggestions to change the variable PYDEVD_WARN_SLOW_RESOLVE_TIMEOUT in settings.json:
"launch": {
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env": {
"PYDEVD_WARN_SLOW_RESOLVE_TIMEOUT": "10"
}
}
],
"compounds": []
},
I also tried to apply the same to launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env": {"PYDEVD_WARN_SLOW_RESOLVE_TIMEOUT": "10"},
"cwd": "${fileDirname}"
}
]
}
This is the VS Code version:
Version: 1.70.2 (user setup)
Commit: e4503b30fc78200f846c62cf8091b76ff5547662
Date: 2022-08-16T05:35:13.448Z
Electron: 18.3.5
Chromium: 100.0.4896.160
Node.js: 16.13.2
V8: 10.0.139.17-electron.0
OS: Windows_NT x64 10.0.19042
Do you see anything wrong? any other suggestions?

Time out waiting for launcher to connect in VS code

I did python debugging in VS code.
The following is the launch.json file:
{
// 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",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"python": "${command:python.interpreterPath}",
"program": "${file}",
"cwd": "${workspaceFolder}",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions":[
"RedirectOutput"
],
"console": "integratedTerminal"
}
]
}
The following is settings.json file:
{
"python.pythonPath": "c:\\Users\\susan\\Documents\\PythonScripts\\venv\\Scripts\\python.exe",
// to fix 'Timeout waiting for debugger connections'
"python.terminal.activateEnvironment" : false
}
When I debug the python script in VS code, I got Time out waiting for launcher to connect and cannot debug the python script.
May I know how can I solve this issue?
Its very simple. Open the launch.json file and add the following into it:
{
"name": "Python: Debug Console",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "internalConsole"
}
Then save and exit it. Whatever you do, DO NOT clear the text already in there or else it may make it worser
If for whatever reason "internalConsole" isn't a good solution:
in your shell script:
export PROCESS_SPAWN_TIMEOUT=30
or just hack the code directly (will be reverted if you update the extension):
.vscode-server/extensions/ms-python.python-2022.18.2/pythonFiles/lib/python/debugpy/adapter/launchers.py[161]:
change:
timeout=(None if sudo else common.PROCESS_SPAWN_TIMEOUT)
to:
timeout=30,

How to configure vimspector.json to work with poetry run

I'm building a CLI in python using poetry and have used vimspector separately in the past for python debugging.
When I test a normal python file, I can set up my vimspector.json like
{
"configurations": {
"run": {
"adapter": "debugpy",
"configuration": {
"request": "launch",
"protocol": "auto",
"stopOnEntry": true,
"console": "integratedTerminal",
"program": "${workspaceRoot}/gzrparse.py",
"cwd": "${workspaceRoot}"
}
},
"parse": {
"adapter": "debugpy",
"configuration": {
"request": "launch",
"protocol": "auto",
"stopOnEntry": true,
"console": "integratedTerminal",
"program": "${workspaceRoot}/test/parse.py",
"cwd": "${workspaceRoot}"
}
}
}
}
Which works fine for those purposes, I would love to debug my CLI with vimspector as well, but i'm struggling to find the correct configurations in my .json file. When on the CLI I can poetry run packagename which works fine, so i'm trying to get vimspector to replicate that command.
Does anyone know if that's possible? Or how to do it?
I've been trying to adapt from this answer but I'm a little out of my depth on what i'm doing.
First you have to execute poetry install to create virtualenv and install your module in it.
Then you can start debugging a file you are editing using the configuration:
"run-current-file": {
"adapter": "debugpy",
"variables": {
"venv": {
"shell": ["poetry", "env", "info", "-p"]
}
},
"configuration": {
"request": "launch",
"protocol": "auto",
"stopOnEntry": true,
"console": "integratedTerminal",
"python": "${venv}/bin/python",
"program": "${file}",
"cwd": "${workspaceRoot}"
}
},
This will start debugging the currently open file using python from poetry virtual environment found by executing command poetry env info -p
Of course you can leave the "program" setting to always debug the same file:
"program": "${workspaceRoot}/test/parse.py"
To debug a module (provided your module has __main__.py file) you need configuration:
"runmodule": {
"adapter": "debugpy",
"variables": {
"venv": {
"shell": ["poetry", "env", "info", "-p"]
}
},
"configuration": {
"request": "launch",
"protocol": "auto",
"stopOnEntry": true,
"console": "integratedTerminal",
"python": "${venv}/bin/python",
"module": "<mymodule>",
"cwd": "${workspaceRoot}"
}
},
Where have to replace <mymodule> with your module name.

Categories