Skip to content
Advertisement

Python in VSCode: Set working directory to python file’s path everytime

So I want to migrate from Spyder to VSCode, and I come across this problem where I cannot access a dataset as my working directory is not the same as the dataset’s path.

launch.json is not auto-generated for me as I’m not debugging anything (I tried this).

How do I set the working directory to always be the dir of the Python file I want to run, in VSCode? (and if it’s bad practice, could you show me a config that is easy to work with?) I’d like to set this up for VSCode’s IPython terminal.

Advertisement

Answer

Updated Solution: working as of 21/1/2021

Option 1:

  1. Locate and open the user settings file:
    • Windows %APPDATA%CodeUsersettings.json
    • macOS $HOME/Library/Application Support/Code/User/settings.json
    • Linux $HOME/.config/Code/User/settings.json
  2. Add this line: "python.terminal.executeInFileDir": true

Option 2:

  1. Open the Settings editor:
    • On Windows/Linux – File > Preferences > Settings
    • On macOS – Code > Preferences > Settings
    • or use the keyboard shortcut (Ctrl+,).
  2. Check the following box:
    • Extensions > Python > Terminal: Execute In File Dir.
    • or use the Search bar and type this setting id python.terminal.executeInFileDir.
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement