简体   繁体   中英

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.

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".

The current latest solution according to other questions is to go to the python extension settings and tick 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.

Anybody has a solution for it?

You could try changing the content inside launch.json from

            "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).

You needn't change the cwd configuration. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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