Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 680
Inyoung Kim 김인영 Avatar asked Aug 12 '19 04:08

Inyoung Kim 김인영


People also ask

How do I run a specific file in VS Code?

To run or debug a simple app in VS Code, select Run and Debug on the Debug start view or press F5 and VS Code will try to run your currently active file.

How do I open a VS Code module?

To open one of these files, use Tab again to pick the file you want to navigate to, then release Ctrl to open it. Alternatively, you can use Ctrl+Alt+- and Ctrl+Shift+- to navigate between files and edit locations.

How do I change the default path in Python VS Code?

To do so, open the Command Palette (Ctrl+Shift+P) and enter Preferences: Open User Settings. Then set python.condaPath , which is in the Python extension section of User Settings, with the appropriate path.

What is Ptvsd?

GitHub - microsoft/ptvsd: Python debugger package for use with Visual Studio and Visual Studio Code.


1 Answers

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}",
    }
  ]
}
like image 167
Jackiexiao Avatar answered Oct 23 '22 17:10

Jackiexiao