Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 578
Fawwaz Yusran Avatar asked Jun 26 '19 15:06

Fawwaz Yusran


People also ask

How do I change the default path in python VS Code?

To do so, open the Command Palette (Ctrl+Shift+P) and enter Preferences: Open User Settings. Then set python.condaPath , which is in the Python extension section of User Settings, with the appropriate path.

How do I change the path of a directory in Python?

To change the current working directory(CWD) os. chdir() method is used. This method changes the CWD to a specified path. It only takes a single argument as a new directory path.

How do I change the working directory in VS Code?

Just close Visual Studio Code and open it in the right folder again. Press Command + O and open your folder.


1 Answers

I think the setting you need is here:

File > Preferences > Settings > Python > Data Science > Execute in File Dir

like image 69
brch Avatar answered Nov 16 '22 00:11

brch