I’ve just started coding for a class and I’m trying to get used to VS Code, but I’m having some issues running a python program from the terminal. I figured out how to change the powershell terminal’s cwd to the correct folder that I’m storing my projects in; however when I create a new python terminal, the cwd is not correct.
I’ve already changed the "Terminal > integrated Cwd" setting to the correct file directory and I’ve tried changing the "Python > Testing: Cwd"; but opening a new python terminal still displays the wrong Cwd.
I read that a solution to a similar issue was to edit a .JSON file for VS code; but I’m fairly certain that this is a user issue as I just don’t know what I’m doing in VS Code yet. Any help would be much appreciated!
3
Answers
You can use the
os
python package to check your current working directory and project scope.Use either the
F5
key or thecd Week_1
command, depending on what you need.If you just want to run the Python script you currently have open, you don’t need the terminal at all. You can use the
F5
key (debug) orCTRL+F5
(run without debugging). This will immediately run the script, but it won’t necessarily open the terminal in the directory you want.If you need the terminal to be open in a specific directory, the easiest solution is just to use the command line IMHO. The
cd {path}
command will switch your working directory to whatever you pass aspath
, so you can use it to navigate to another folder in the terminal. In your case, after opening your Python terminal, typecd Week_1
and you should get to theWeek_1
directory.You could use debug mode and set
cwd
in yourlaunch.json
.See document about VSCode Debugging for more details.
If you just want to execute the scripts in the file’s directory, you could check Execute In File Dir option in your Settings.