Home > Back-end >  Visual Studio Code can't run Python propertly
Visual Studio Code can't run Python propertly

Time:10-18

After windows re-installation I've met a strange problem with running python scripts from Visual Studio Code. I can run simple helloworlds but if im trying to open a file for example simple txt from the same directory as working script, it returns me that file is not found. At the same time the same script works well if I run it just from command line. Ive discovered that the problem is exactly with VSCode while running script from VSC it cant properly determine working directory of the script. Ive never met such a problem before and I cant find a solution.

CodePudding user response:

Here are a couple solutions I found:

  1. Try to give the exact location of the file. Something like this: C://user/desktop/folder/file.txt
  2. Create a enter image description here

    So you can use the absolute path to avoid it or to modify the cwd. But both of them are not suggested. You should depend on the default cwd(workspace folder path) to reference the file.

    Such as, if you move the file under the workspace folder directly, your code should work, otherwise, you need to add the prefix path before the file you want to using.

  • Related