Exception has occurred: ImportError Vscode While Debugging - python

I am trying to try the debug tool in my vs code I am totally new to vs code and I am getting an error such as this when I run it
ERROR MESSAGE I have a folder called .vscode with a launch.json file `
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\myPro\\manage.py",
"args": [
"runserver"
],
"django": true,
"justMyCode": true
}
]
}
I watched a lot of videos on youtube and I see people who have settings.json in there .vscode file.
WHAT I HAVE DONE:
I have downloaded django == 3.25
I have downloaded python == 3.10.4
IN a virtual environment my environment works perfectly fine its just that I can find the reason to this problem I would really appreciate it if someone can help me out

Try Ctrl + Shift + P and the option "Select Python Interpreter", then select the one in which the environment is. This should have VSCode running in the same environment.

Please change your debug settings to your Virtual Environment
Open .py file and then debug it.
You may have to restart vscode

Related

VS Code Debugger within Docker Dev Container suddenly stopped working

I've been using VS Code combined with Docker as a Dev container without problems for weeks. But now the debugger has stopped working on any files I've been working on. It works fine if a create a new file that contains a couple of print() statements. But not on any files I've been committing/pushing to GitHub (and which have also been edited by other people using PyCharm).
I'm using Python, I have the Python and Pylance extensions installed (along with others). Here's my launch.json, any ideas?
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"python": "/usr/local/bin/python"
}
]
}
Update:
I can run the problem file and set a breakpoint at the imports. But it doesn't work for anything below the import statements and also not in an if __name__ == '__main__' clause

VSCode Python debugger no longer stopping at breakpoints after moving project directory

I just moved my project to a new directory on my computer. The debug config executes but no longer stops at breakpoints.
When I move the project folder back to its original location, it's working again.
Thanks for your help!
My launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "Module Python",
"type": "python",
"request": "launch",
"cwd": "${workspaceFolder}",
"module": "bin.main",
"justMyCode": true
}
]
}
Config :
MacOS 10.15
VSCode 1.69.2
Conda virtual env with python 3.7.13
python extension ms-python.python-2022.13.12381007

vscode returning "Timed out waiting for launcher to connect" error, python runs perfectly in other text editors and in terminal

I got this error in VSCode, saying that it timed out waiting for the launcher to connect. I've tested it and Python works perfectly fine in command prompt and in other text editors. This is what it says: VSCode error This is what my launch.json file is:
{
// 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"
},
{
"type": "pwa-chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}
I don't know what is wrong or how to fix it. It was working perfectly before and then it simply threw that error when it was running.
Sorry, but it's a little weird of your debug command. Could you try to delete the Debug Console and try it again? And if it still does not work, could you attach the full outputs from the first line of the Debug Console?

VS Code: ModuleNotFoundError when using debug

I develop a Django project
I ma newbie in Django and VS Code so I "debug" using print() and console.log() which is not efficient...
so I really need to be able to debug my code and follow variable value during execution but when I try to run debug
I read the VS Code doc about django
so I have set the launch.json like that:
{
// 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 : Fichier actuel",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload"
],
"django": true
},
]
}
I select Python: Django in the debug configuration list and run my project and have ModuleNotFoundError: No module named 'simple_history' and other...
I don't understand because my project works so tierce app are correctly installed in my env
It wonder if it could be link to another problem I have since I start using python/VS Code and was not able to fix (I already post on this problem: python can't load on VS Code):
The environment variable 'Path' seems to have some paths containing the '"' character. The existence of such a character is known to have caused the Python extension to not load. If the extension fails to load please modify your paths to remove this '"' character.
Someone can help me making debug operational?
thanks !

VSCode -- how to set working directory for debugging a Python program

How do I run a Python program under debug and set the working directory for the run?
#SpeedCoder5's comment deserves to be an answer.
In launch.json, specify a dynamic working directory (i.e. the directory where the currently-open Python file is located) using:
"cwd": "${fileDirname}"
This takes advantage of the "variables reference" feature in VS Code, and the predefined variable fileDirname.
If you're using the Python: Current File (Integrated Terminal) option when you run Python, your launch.json file might look like mine, below (more info on launch.json files here).
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}"
},
//... other settings, but I modified the "Current File" setting above ...
}
Remember the launch.json file controls the run/debug settings of your Visual Studio code project; my launch.json file was auto-generated by VS Code, in the directory of my current "Open Project". I just edited the file manually to add "cwd": "${fileDirname}" as shown above.
Remember the launch.json file may be specific to your project, or specific to your directory, so confirm you're editing the correct launch.json (see comment)
If you don't have a launch.json file, try this:
To create a launch.json file, open your project folder in VS Code (File > Open Folder) and then select the Configure gear icon on the Debug view top bar.
Per #kbro's comment, you might be prompted to create a launch.json file by clicking the Debug button itself:
When I clicked on the Debug button on my navigation panel it said "To customise Run and Debug create a launch.json file." Clicking on "create..." opened a dialog asking what language I was debugging. In my case I selected Python
Configure the cwd setting in launch.json as follows:
{
"name": "Python",
"type": "python",
"pythonPath": "python",
...
"cwd": "<Path to the directory>"
...
}
This setting helps me: (I am a Windows person)
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"cwd": "${workspaceFolder}\\app\\js", // set directory here
"program": "${workspaceFolder}\\app\\js\\server.js", // set start js here
}
In some cases, it might be also useful to set the PYTHONPATH along with the workspaceFolder:
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH": "${cwd}"
}
}
I am posting this sample configuration for people who use TypeScript on Node.js
in my project my Node.js server TypeScript files are located in folder Application_ts
and the compiled js files are generated in the folder named Application
because when we run our application in debug mode or start it normally we should start from Application folder which contains the js files
so bellow configuration run debug from root folder where my application_ts also exists and works perfect
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug TypeScript in Node.js",
"program": "${workspaceRoot}\\Application\\app.js",
"cwd": "${workspaceRoot}\\Application",
"protocol": "inspector",
"outFiles": [],
"sourceMaps": true
},
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858,
"outFiles": [],
"sourceMaps": true
}
]
}
You can set up current working directory for debugged program using cwd argument in launch.json
To set current working directory to whatever file you are executing at the time:
File > Preferences > Settings > Python > Data Science > Execute in File Dir
Thanks brch: Python in VSCode: Set working directory to python file's path everytime
I faced the same issue and noticed that when running the which python command in Terminal in Mac it shows me a different path to what I get when I run the which python command in vs code. And also that my file runs properly in the terminal when run using python filename.py
So I copied that path from the terminal and pasted it in VS code into Preferences->Settings->Extensions->Python->Default Interpreter Path and it worked. I hope this helps.
I use the "justMyCode = false" so I can also debug and jump into the functions that the main script calls.
{
// 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": false,
"cwd": "${fileDirname}" }
]
}

Categories