pythonvisual-studio-codejupyter

How to set the running file path of jupyter in VScode?


When I use the jupyter extension in VScode and run a line of code in jupyter to save a file using relative path,I found the file(iris_tree.dot) in another file. It's just like i debug/run the code in another file path. How can I set the correct path of the jupyter runner?

#%%
from sklearn.tree import export_graphviz
export_graphviz(
tree_clf,
out_file="iris_tree.dot",
feature_names=iris.feature_names[2:],
class_names=iris.target_names,
rounded=True,
filled=True
)

Solution

  • I'm one of the developers on this extension. By default we follow the VSCode pattern of working directory as opposed to the Jupyter pattern. Meaning that we use the root of the currently open workspace folder as the current working directory for starting jupyter notebooks. That might be what is confusing you here.

    To get around this you can either set cwd in your notebook code as redhatvicky mentioned or you can change the default current working directory in the following VSCode setting.

    Jupyter -> Notebook File Root

    Since you can change that setting per workspace you can have it always default to a specific location when working in just the workspace that contains your file.

    Edit 2/16/22 New setting location