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

Question:

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
)
Asked By: YANGSU LIU

||

Answers:

Your question seems quite confusing and I am unable to post a comment. Please follow this link. As per your question, I think the issue is you need to select the correct python interpreter by CTRL+SHIFT+P and then Python: Select Interpreter to select the correct conda environment or a conda interpreter. Otherwise you can try and execute the following code to change your directory before any other command:

import os
try:
    os.chdir(os.path.join(os.getcwd(), 'path_to_folder_to_have_the_file')) # '.' if the path is to current folder
    print(os.getcwd())
except:
    pass
Answered By: Harsh Gupta

Generally you can change the working directory by using “os.chdir(NEW_PATH)”

One another Suggestion is that , You can set the location to save the image from the code itself.

Here below is the code which might help you.

from __future__ import division, print_function, unicode_literals

# Common imports
import numpy as np
import os

# to make this notebook's output stable across runs
np.random.seed(42)

# To plot pretty figures
import matplotlib.pyplot as plt

plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12

# Where to save the figures
PROJECT_ROOT_DIR = "."
CHAPTER_ID = "decision_trees"

def image_path(fig_id):
    return os.path.join(PROJECT_ROOT_DIR, "images", CHAPTER_ID, fig_id)

def save_fig(fig_id, tight_layout=True):
    print("Saving figure", fig_id)
    if tight_layout:
        plt.tight_layout()
    print(image_path(fig_id) + ".png")
    plt.savefig(image_path(fig_id) + ".png", format='png', dpi=300)

save_fig("Fig-01-6TFG")
Answered By: redhatvicky

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

Answered By: Ian Huff

Just update the value of "Notebook File Root" to ${workspaceFolder} or ${fileDirname}.

enter image description here

Answered By: Mike Stop Continues

@Ian Huff’s answer is still valid, however the setting seems to have changed location since then.

Instead of "Python -> Data Science -> Notebook File Root", it’s now "Jupyter -> Notebook File Root"

Answered By: Jazzman