How to make VScode launch.json for a Python module - python

I'm reseaching self-supervised muchine learning code.
And I have wanted to debug the code with python debugger not pdb.set_trace().
This is python command for ubuntu terminal.
python -m torch.distributed.launch --nproc_per_node=1 main_swav.py \
--data_path /dataset/imagenet/train \
--epochs 400 \
--base_lr 0.6 \
--final_lr 0.0006 \
--warmup_epochs 0 \
--batch_size 8 \
--size_crops 224 96 \
--nmb_crops 2 6 \
--min_scale_crops 0.14 0.05 \
--max_scale_crops 1. 0.14 \
--use_fp16 true \
--freeze_prototypes_niters 5005 \
--queue_length 380 \
--epoch_queue_starts 15\
--workers 10
In order to debug the code with VScode, I tried to revise launch.json like below as referring stackoverflow -question
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"module": "torch.distributed.launch --nproc_per_node=1 main_swav.py",
"request": "launch",
"console": "integratedTerminal",
"args": ["--data_path", "/dataset/imagenet/train"]
}
]
}
I knew this would not work... TT
Could you give me some advice?
Thank you for your time.

Specify the module you want to run with "module": "torch.distributed.launch"
You can ignore the -m flag. Put everything else under the args key.
Note: Make sure to include --nproc_per_node and the name of file (main_swav.py) in the list of arguments
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"module": "torch.distributed.launch",
"request": "launch",
"console": "integratedTerminal",
"args": [
"--nproc_per_node", "1",
"main_swav.py",
"--data_path", "/dataset/imagenet/train",
]
}
]
}
Read more here: https://code.visualstudio.com/docs/python/debugging#_module

This is an example of my launch.json that I use to debug Python modules.
It has an additional configuration to debug "current file" (not as module) which is useful to keep.
{
linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "path.to.module",
"args": ["run_example --arg <arg>"],
"justMyCode": true
},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
This would replicate a terminal command to run a Python module like so:
python -m path.to.module run_example --arg <arg>

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
}
]
}

vscode python launch 'current file' as module

I am trying to find a way to give current file as argument for module. I couldn't find it anywhere so I'm trying stackoverflow
{
"name": "Python: Synapse",
"type": "python",
"request": "launch",
"module": "projFolder.targetModule",
"console": "integratedTerminal"
}
Manually specifying the module argument such as above example, works. But thinking of doing that for every each file wanted me to automate it. I've tried ${file}, but it gives the file path not the module. So it didn't work. How can I launch current current file as module?
ps: answer from https://stackoverflow.com/a/57416114/6088796, but I modify a litter
add something like this to your launch.json, but if you want to run multi-module(in a different place, the only way you can do is using multi configuration, or use a plugin(see below)
"configurations": [
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "yourmodule.${fileBasenameNoExtension}",
"stopOnEntry": true
},
You can use the extension Command Variable to get this relative directory with dot separator.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module CmdVar",
"type": "python",
"request": "launch",
"console": "integratedTerminal",
"module": "${command:extension.commandvariable.file.relativeDirDots}.${fileBasenameNoExtension}",
}
]
}

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.

How to start python in "-X dev" mode when debugging / running from terminal?

python3 supports -X dev startup mode.
This feature is becoming very useful for code "environment awareness" and for execution functionality.
For ex. the debug prints of asyncio module https://docs.python.org/3/library/asyncio-dev.html#debug-mode-of-asyncio
How can I configure vscode to start python with this flag ?
vscode now supports pythonArgs in launch.json, but note that it expects a format of ['-X', 'dev']:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"pythonArgs": ["-X", "dev"],
},
]
}

Categories

Resources