0

(I'm currently learning Python so I might be missing something obvious - sorry if this is the case)

I'm using Visual Studio Code and the AREPL extension (to get an idea of what will show up while I'm writing code) to learn Python coding

However, I encountered a weird error, where different working directories seem to be registered for AREPL and the console (sorry I'm new so my descriptions and wording might be inaccurate)

Specifically, if I type

cwd = os.getcwd()
print(cwd)

on the AREPL panel, it shows the folder containing the python file I'm working on as the working directory (C:\Users\XXXXXX\Coding\Python),

but when I actually run the code, the console shows the upper folder (directory?) (C:\Users\XXXXXX\Coding )

So when when I try to open different files in the C:\Users\XXXXXX\Coding\Python folder using a relative path (e.g. open("temp.txt", "r")) then it runs without error on the AREPL window and displays the expected results, but shows an error when I actually run it through console

This is also weird because I remember having used relative paths in the past without problem when I was working on a file in a different folder

FYI, I never changed any settings related to working directory or manually set them until now

I know that the obvious easy answer is to use an absolute path, but I'm just trying to figure out what might be causing the error to better learn about Python (or Visual Studio Code)

Thank you!

2
  • you can always change the current directory in the console/terminal
    – rioV8
    Feb 6, 2021 at 10:16
  • @rioV8 Thanks! I also know that (I should have mentioned) but I guess I was just more curious why this happened so that I could understand what's going on and potentially guard against future problems :)
    – PRS_CU24
    Feb 7, 2021 at 11:28

1 Answer 1

0

The reason is that the precise locations of the startup paths displayed by default on the two terminals are different.

In the default terminal of VS Code, the startup path displayed by default is the currently opened project folder. When a file is executed, it will automatically go to this file and then return to the project folder. This is convenient when executing any file in the project in this terminal, VS Code can go to it without reopening this terminal.

The "AREPL" terminal executes this python file, and it displays the parent folder of the executed file by default. When we switch other executable files, the "AREPL" terminal needs us to reopen this terminal.

enter image description here

3
  • Thanks for the detailed reply! I really appreciate it! However, if I understand correctly, you're saying that the default VS Code working directory should be the folder my file is located in and the AREPL working directory should be the parent folder. In my case, it's the other way around, where the default VS Code working directory is the parent folder and the AREPL working directory is the folder my file is located in. I don't want to bother you since this is a minor question that I have easy workaround for, but I was just puzzled and curious :) Thanks again!
    – PRS_CU24
    Feb 10, 2021 at 1:04
  • @PRS_CU24 -In my test, the default path of VS Code's default terminal is the project folder(\test01), and the "AREPL" terminal defaults to the parent folder of the executable file(\test01\demo). Have you set some related paths in "settings.json"?
    – Jill Cheng
    Feb 10, 2021 at 1:17
  • I don't think so (I'm pretty new to python so I try not to mess with settings I know I'm familiar with). Anyways, thanks again for your time and comment! I don't want to further bother you as this seems to be an individual issue that isn't shared by everyone :)
    – PRS_CU24
    Feb 10, 2021 at 3:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.