0

I am using vscode to write Python and when I run .py files they run from wirectory of workspace.
How can I get the file to run from the files dricetory instead of workspace directory?

I have tried setting this on: python.terminal.executeInFileDir in settings but that did not change anything. On https://code.visualstudio.com/docs/python/settings-reference their explanation of this is exactly what I am trying to do but it does not work.
I have also tried several different variables in launch.json such as "cwd": "{$fileDirname}, "cwd": "{$file}, "cwd": "{$fileDirname}.
My launch.json:

{
// 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", 
        "cwd": "{$fileDirname}"
    }
]

}

vetle
  • 1
  • 1
  • Where do you place those `"cwd"` key-value pairs? Can you share your whole launch.json with us? – Károly Szabó Oct 16 '20 at 15:23
  • 2
    The `"cwd": "${fileDirname}"` option should work – Károly Szabó Oct 16 '20 at 15:26
  • 1
    launch.json is now added to post. – vetle Oct 16 '20 at 15:54
  • analyze the command typed in the terminal to see what you should put in the `cwd` field – rioV8 Oct 16 '20 at 19:24
  • @vetle your launch.json seems right. Another way to possibly solve your problems is making you script current working directory independent, everywhere you are using a path you can get it relative to the current python file using this approach: https://stackoverflow.com/questions/595305/how-do-i-get-the-path-of-the-python-script-i-am-running-in. If nothing works maybe try to update or reinstall your vscode. – Károly Szabó Oct 19 '20 at 11:34

1 Answers1

0

When I set "python.terminal.executeInFileDir": true, in "settings.json", and open a new VSCode terminal, VSCode automatically enters the folder directory where the currently executed script is located when the file is executed:

enter image description here

Jill Cheng
  • 5,009
  • 1
  • 4
  • 16