Current Working Directory For Jupyter Notebook Sets To Temp Folder In Vscode
Solution 1:
As of January 2021 adding the following line in my setting helps to solve the issue
"jupyter.notebookFileRoot": "${workspaceFolder}",
Solution 2:
I'm also affected by this issue. Here is how I solved it.
Using the configuration parameter of ${fileDirName}
in Python > DataScience: Notebook File Root, has this effect as I could check in my environment.
If I open an Python Interactive Window using the commands Ctrl+Shift+P > Python:Show Python Interactive Window, and then run:
import os
os.getcwd()
The output is a random temporal folder. So I couldn't do imports cause my local relative modules where not found.
However, If instead of opening directly a fresh Python Interactive Window I run a cell of code of any of my python files
#%%
print("Some string here")
and then find the current working path, it changes to the fileDirName
as expected. Then any interaction with the interactive window will perform correctly (and my next imports will perform correctly).
To avoid this behaviour and because I would like to perform operations within my workspaceFolder I changed Jupyter: Notebook File Root to ${workspaceFolder}
. In this case opening a new fresh Python Interactive Window, will set the current path automatically to the workspaceFolder, with and without the need to execute any python cell.
I think that, when using ${fileDirName}
, it would be good to set the current working open file folder as the working path of the Python Interactive Window (Jupyter) instead of the temporal random folder (In the case of opening without executing a specific cell), but doesn't look like it behaves like that.
Hope it were useful!
Since Nov 2020 the Jupyter extension is separated from Python extension for VS Code. The setting key has been renamed from python.data science to jupyter
Solution 3:
To answer your third question, try this:
import os
os.system("echo %cd% > dir")
file = open("dir", "r")
filePath = file.read()
file.close()
print(filePath.split("\n")[0])
Solution 4:
I had the same problem. I solved it by putting the setting into the settings.json
file manually:
{
"jupyter.notebookFileRoot": "${fileDirname}",
}
Why it solves the issue (my guess):
Jupyter changes the working directory when starting. After this, the notebookFileRoot settings from settings.json
files are implemented, however as ${fileDirname}
is default, this is not added to the json file. Therefore the file root will not be changed back to ${fileDirname}
if the setting is set through the UI settings in vscode.
Post a Comment for "Current Working Directory For Jupyter Notebook Sets To Temp Folder In Vscode"