简体   繁体   English

VSCode 在终端中将工作目录更改为 python 文件路径

[英]VSCode change working directory to python file path in terminal

Before somebody asks yes this is a duplicate but all the solutions were 2-3 years old and I tried them all.在有人问是的之前,这是重复的,但所有的解决方案都是 2-3 岁,我都试过了。

The problem is that my launch.json is in the parent directory, so if I execute a file in a child directory and access a file stored in that child file directory, python throws an error, "File not Found".问题是我的 launch.json 在父目录中,所以如果我在子目录中执行文件并访问存储在该子文件目录中的文件,python 会引发错误,“找不到文件”。

The current latest solution according to other questions is to go to the python extension settings and tick Terminal: Execute in File Dir .根据其他问题的当前最新解决方案是将 go 到 python 扩展设置并勾选Terminal: Execute in File Dir which I already did.我已经这样做了。 But the working directory is still not correct.但是工作目录仍然不正确。 I tried os.getcwd() and it still returns the path of the parent directory.我尝试了 os.getcwd() ,它仍然返回父目录的路径。

Anybody has a solution for it?有人有解决方案吗?

You could try changing the content inside launch.json from您可以尝试更改launch.json中的内容

            "cwd": "${fileDirname}"

to this对此

            "cwd": ""

cwd:密码:

Specifies the current working directory for the debugger, which is the base folder for any relative paths used in code.指定调试器的当前工作目录,它是代码中使用的任何相对路径的基本文件夹。 If omitted, defaults to ${workspaceFolder} (the folder open in VS Code).如果省略,则默认为 ${workspaceFolder}(在 VS Code 中打开的文件夹)。

You needn't change the cwd configuration.您无需更改cwd配置。 Just need to modify the relative path to the file which you want to reference.只需修改要引用的文件的相对路径即可。

You can refer to the official docs , it has a detailed example.你可以参考官方文档,里面有详细的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM