Home > Enterprise >  How to make VSCode run your code from the location of the code
How to make VSCode run your code from the location of the code

Time:12-21

I keep on getting Module Not Found errors in Python and by printing out os.getcwd(), I found that VSCode seems to be running things from the workspace folder (the top-level folder in the editor) rather than the folder that the code is in. Thus, it can't find the the modules because all of my relative import and path-changing code doesn't work.

I did some research and found the option "python.terminal.executeInFileDir": true in settings, which seems like it should change this, but when I press F5, the code still seems to be run out of the workspace folder.

How can I get the code to be run out of the same folder that the file is in when I run my code?

CodePudding user response:

When pressing F5, you're using the debugger.

The debugger's settings are separate from the "python.terminal.executeInFileDir": true, and can be changed by creating a launch.json file (or editing the existing one) as described in more detail here.

Specifically, you will want to add "cwd": "${file}/.." to your launch.json file. This will set the directory for debugging to be the encapsulating folder of the file you are running.

CodePudding user response:

And you can add "cwd": "${fileDirname}" in the lanuch.json file.

  • Related