How do you run a python program in visual studio - python

Whenever I hit the f5 key to run a program a new file called launch.json opens and I don't know what to do from there. Does anyone know hat to do and no this is no the same as how do I install python for visual studio.
This is what launch.json looks like.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"program": "${file}",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
},
{
"name": "Python Console App",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"program": "${file}",
"externalConsole": true,
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit"
]
},
{
"name": "Django",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"program": "${workspaceRoot}/manage.py",
"args": [
"runserver",
"--noreload"
],
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput",
"DjangoDebugging"
]
},
{
"name": "Watson",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"program": "${workspaceRoot}/console.py",
"args": [
"dev",
"runserver",
"--noreload=True"
],
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
}
]
}
Thanks in advance !

I guess you meant VS Code. If that's the case, this is an auto generated file where you can configure what will happen when you press F5. If you save that file as is and then switch back to your main.py or whatever python file you want to debug and press again F5 you will start debugging with a breakpoint on your first executable line.

Related

VSCode not debugging correctly gunicorn

So after much searching I found a way to get gunicorn to play nice with vscode using the following launch.json
"version": "0.2.0",
"configurations": [
{
"name": "Python: Run Debug",
"cwd": "${workspaceFolder}/dir/dir2",
"type": "python",
"request": "launch",
"program": "/Users/me/.virtualenvs/dir/bin/gunicorn",
"args": ["--bind 0.0.0.0:6543", "-w=1", "--paste=local.ini" ],
"console": "integratedTerminal",
"justMyCode": false,
"purpose": ["debug-in-terminal"],
"gevent": true,
"env": {
"GEVENT_SUPPORT": "True",
"PYTHONUNBUFFERED":"1",
"OBJC_DISABLE_INITIALIZE_FORK_SAFETY":"YES",
"PYDEVD_LOAD_NATIVE_LIB": "0",
"PYDEVD_USE_CYTHON":"0"
}
}
]
}
Now this makes the server run but it outputs no variables, it doesn't stop at any breakpoints, and I can't watch anything. Basically, the whole left pane is completely empty.
Has anyone dealt with this successfully before?

python don't save files in visual studio code

I started to use visual studio code yesterday and i'm having this problem...
Let's suppose I have this code:
myFile = open("file.txt", "w+")
myFile.write("something \n")
myFile.close()
When I run it on VScode it don't save the file, but when I run it in SublimeText it does.
do you know how to solve it?. I didn't find anything (or I don't know how to search), my launch.json look like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "internalConsole"
}
]
}
**After I wrote that I noticed that it saves the file in "C: \ Users \ User \ Documents \ vsprofiles", but I have my .py file on the desktop, I want it to save the file in the same path where the .py file is, like SublimeText does.
For completeness here is my config file
"launch": {
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}",
"env": {},
"pythonArgs": [],
"stopOnEntry": false
}
]
}

timeout error when using external terminal in Manjaro

I have installed Visual Studio Code with the Python extension in Linux Manjaro. When I try to launch a Python script, the external terminal opens but after 5s I get an error message in a window telling me "timeout" and my script doesn't launch.
I've seen this post with the same problem on Windows 10 but the fix doesn't seem to work on Manjaro. Any idea?
Here is my 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 (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
},
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "enter-your-module-name-here",
"console": "integratedTerminal"
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/IA_TD2.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"
},
{
"name": "Python: Current File (None)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "none"
}
]
}
Unfortunately VSCode is not compatible with every terminal and it seems not (yet) compatible with your Linux Manjaro installation with KDE Desktop Environment.
Switching to another terminal will surely solve this (i.e. GNOME Terminal). I'm not sure, if the gnome-terminal package is aviable without installing the GNOME Desktop Environment.
Here is a good solution how you can do this without reinstalling the whole OS.

External Terminal Debugger in VS Code

As I can't give input in the debug console, I'm trying to run the debugger in External terminal in VS Code.
This is the part of launch.json config file for external terminal.
{
"name": "Python: Terminal (external)",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"cwd": "",
"console": "externalTerminal",
"env": {},
"externalConsole": true,
"envFile": "${workspaceRoot}/.env",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit"
]
},
I added the "externalConsole": true part as they said here and I tried with or without that statement.
I get this error,
Debug adapter process has terminated unexpectedly
I tried the docs and the IntelliSense in the json file, but I can't understand and get it to work.
Adding "console": "externalTerminal", to the debug configuration file worked fine on Linux!
I am using windows, however, this should fix your problem.
"name": "Python: Terminal (external)",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "C:/Users/Zac/Anaconda3/python.exe",
"program": "${file}",
"cwd": "",
"console": "externalTerminal",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
},
You need to correctly add a path to your python.exe location in the "pythonPath" line.
Also, take out the "WaitOnAbnormalExit" and "WaitOnNormalExit" from "debugOptions" and just use "RedirectOutput". Remove "externalConsole": true from the code.
Everything else should stay the same.
Hope that helps.
Cheers.
Also remember to put correct value for external terminal location in :
terminal.external.windowsExec

Visual studio code python debugger breakpoint won't trigger

I can't figure out why vscode debugger is not working. I tried everything, but the breakpoints are still not working. Has anyone had a similar problem, and solved it?
My lauch.json file just for reference:
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config.python.pythonPath}",
"program": "${file}",
"cwd": "${workspaceRoot}",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
},
"stopOnEntry": true,
Set this to false

Categories

Resources