skip to Main Content

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

3

Answers


  1. 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()
    
    Login or Signup to reply.
  2. Use either the F5 key or the cd 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) or CTRL+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 as path, so you can use it to navigate to another folder in the terminal. In your case, after opening your Python terminal, type cd Week_1 and you should get to the Week_1 directory.

    Login or Signup to reply.
  3. 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

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