Juan Cole
Juan Cole

Reputation: 11

How can I change the cwd in a python terminal?

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!

Current Cwds for Powershell and Python Terminals enter image description here

Upvotes: 1

Views: 91

Answers (3)

Anerdw
Anerdw

Reputation: 1885

I'd question the assumption that you need to use the terminal at all.

It seems like you just need to run a Python script. If that's the case, you don't need to open a new terminal instance. Pressing the F5 key (debug) or CTRL+F5 (run without debugging) should run whatever script you currently have open without needing you to change your terminal cwd.

Upvotes: 0

MingJie-MSFT
MingJie-MSFT

Reputation: 9209

You could use debug mode and set cwd in your launch.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.

enter image description here

Upvotes: 1

Tadd Bindas
Tadd Bindas

Reputation: 44

You can use the os python package to check your current working directory and project scope.

import os

# Change the current working directory (cwd) to "/tmp"
os.chdir("/tmp")

# Print the cwd
os.getcwd()

Upvotes: -1

Related Questions