How to run python interactive in current file's directory in Visual Studio Code? How to run python interactive in current file's directory in Visual Studio Code? python python

How to run python interactive in current file's directory in Visual Studio Code?


In "User Settings", use the search bar to look for "python.terminal.executeInFileDir" and set (=) its value to "true" instead of "false".


Update following release 2019.10.44104

Following release 2019.10.44104 of the VS Code python extension, you can now set the python.dataScience.notebookFileRoot to ${fileDirname} to directly start the python interactive window in the directory of the file you're running.

Note that the root directory will not change if you then run code from another file unless you interrupt/restart the kernel (or close VS Code). On this aspect, see the following comment and the corresponding github issue.


For the Python Interactive Window, the setting you're looking for is python.dataScience.notebookFileRoot. However, as explained in this answer to a similar question,

Always opening on the file location (without having to set notebookFileRoot to an absolute path per folder) is not supported via the notebookFileRoot setting. The VSCode variables such as ${fileDirname} are specific to task and debug configuration files (launch.json and task.json).

See also the associated github issue.

As indicated, you can still set this setting to a specific absolute path, which might be enough if you're mainly working on a single project at a time.

Alternatively, you could also add the following code at the top of your script/notebook:

import osos.chdir('absolute-path-to-workingDir')


you need to go to file/preferences/user settings and click the "{}" icon at the top right of the window. After that, put this setting in: "terminal.integrated.cwd": "C:\\Users\\myUser\\", and after that wherever your terminal's directory happens to be. This answer is not the most inaccurate cause im still a noob myself at using vscode so if someone more experienced with it could reply to this thread it would be great.