I am trying to run python code using the jupyter extension in vscode here
I have a workspace open at this location /Users/user/Documents/ When I try and run the following code in a file called test.py in a child directory, the current working directory is set at the workspace level rather than the file. Is it possible to change a setting to use the cwd of the file, rather than the workspace? I cannot find one in settings.json and the "cwd" in launch.json only seems applicable to debugging.
File location: /Users/user/Documents/python_code/test.py
#%%
import os
print(os.getcwd())
Expected output: /Users/user/Documents/python_code/
Actual output: /Users/user/Documents/
When running the same code through the terminal it prints the expected result, so the issue seems to be related to the jupyter extension
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.
Launch Visual Studio 2019 and in the start window, select Open at the bottom of the Get started column. Alternately, if you already have Visual Studio running, select the File > Open > Folder command instead. Navigate to the folder containing your Python code, then choose Select Folder.
For any specific folder / workspace that you have open in VS Code you can use the notebookFileRoot setting to set a specific absolute path directory to always set the Jupyter working directory to when you start the Interactive Window with that folder open.
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). We've specifically added code to recognize ${workspaceFolder} for our settings page but we don't recognize other VSCode variables in there.
If you want there is a github item here that has suggested this feature. You can follow that or vote it up if you want this feature added. https://github.com/Microsoft/vscode-python/issues/4441
Edit 11/5/2019: We have now made a change to allow the VS Code "Notebook File Root" setting to be set to ${fileDirname}. This setting will now start up Notebook Editor and Interactive Window session relative to the file location that was used to start them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With