[英]How to make VSCode run your code from the location of the code
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.我在 Python 中不断收到
Module Not Found
错误,并通过打印os.getcwd()
,我发现 VSCode 似乎是从工作区文件夹(编辑器中的顶级文件夹)而不是代码所在的文件夹中运行的是。因此,它找不到模块,因为我所有的相对导入和路径更改代码都不起作用。
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.我做了一些研究,在设置中找到了选项
"python.terminal.executeInFileDir": true
,这似乎应该改变它,但是当我按下F5时,代码似乎仍然超出了工作区文件夹。
How can I get the code to be run out of the same folder that the file is in when I run my code?当我运行我的代码时,如何让代码从文件所在的同一文件夹中运行出来?
When pressing F5 , you're using the debugger .按F5时,您正在使用调试器。
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 .调试器的设置与
"python.terminal.executeInFileDir": true
是分开的,并且可以通过创建launch.json
文件(或编辑现有文件)来更改,如此处更详细描述的那样。
Specifically, you will want to add "cwd": "${file}/.."
to your launch.json
file.具体来说,您需要将
"cwd": "${file}/.."
添加到您的launch.json
文件中。 This will set the directory for debugging to be the encapsulating folder of the file you are running.这会将调试目录设置为您正在运行的文件的封装文件夹。
And you can add "cwd": "${fileDirname}"
in the lanuch.json file.您可以在 lanuch.json 文件中添加
"cwd": "${fileDirname}"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.