skip to Main Content

In my workingdirectory I have many folders with a python script modifying data in the same folder.
When running Python in VsCode I need to give a relative path from the working directory into the folder. For example using os.getcwd(), test.py is in D:WorkingdirectoryFolder1: VsCode says, D:Workingdirectory. Running it in console: D:WorkingdirectoryFolder1.
Direct path is not an option.
How fo i fix it?
Thanks in advance!

2

Answers


  1. This is caused by vscode using workspace as root floder.

    This will lead to a problem. When you use the os.getcwd() method in the deep directory of the workspace, you will still get the workspace directory.

    You can open your settings and search Python > Terminal: Execute In File Dir then check it.

    enter image description here

    You can also use debug mode and add the following to your launch.json:

    "cwd": "${fileDirname}"
    
    Login or Signup to reply.
  2. If your external terminal works normally, you can also use it as default in vscode by changing the option [Terminal: Explorer Kind] to external.
    screenshoot

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search