21

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
)
YANGSU LIU
  • 213
  • 1
  • 2
  • 6
  • Check this out https://stackoverflow.com/a/56091981/6875391, I've changed the setting "python.terminal.executeInFileDir" (Ctrl+Shift+P search "User setting" then search "python.terminal.executeInFileDir") and it looks like pypath was updated – klapshin Oct 14 '19 at 01:44

5 Answers5

47

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

enter image description here

  • 14
    For quick copy and paste: `${workspaceFolder}` OR `${fileDirname}` – Miladiouss Jan 28 '21 at 00:54
  • I found this more helpful, I didn't see under settings "Python -> data science ..." as @Ian Huff's answer said – Alon Samuel Oct 21 '21 at 16:16
  • 1
    This is the correct answer, at least at this moment. – Javier Huerta Aug 05 '22 at 19:25
  • 2
    If you're having trouble finding this setting, go to the Extensions icon on the left panel of vscode (looks like 4 blocks) and search for Jupyter. Just under the search, on the Jupyter extension, click the gear. Scroll down to "Notebook File Root". – travelingbones Dec 22 '22 at 17:25
  • Another way to find this setting (on Mac) is to go to Code -> Settings -> Settings and search for "Notebook File Root" in the search bar. – chilifan Mar 27 '23 at 12:10
25

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

Ian Huff
  • 2,699
  • 1
  • 17
  • 17
5

@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"

Jazzman
  • 76
  • 1
  • 5
  • This is not a valid answer for the question – Ankit Sangwan Feb 13 '21 at 06:33
  • 4
    Sorry about that @AnkitSangwan. How am I supposed to share that new information concerning a previous answer (since I am not yet allowed to comment on others' answers). – Jazzman Feb 13 '21 at 21:31
  • After you reach a reputation of 50 you will be able to comment. Until then try to answer those questions which don't need any clarification or something. – Ankit Sangwan Feb 14 '21 at 04:36
  • @Jazzman You did the right thing by posting your info as an answer since you were not able to comment. If you have something important to add or tell related to a question, and you cannot comment, there is no other way. *Until then try to answer those questions which don't need any clarification or something* - is not a good suggestion. Most of us do not come here to hunt down questions thinking "if not this then another". We stumble upon a question and if something clicks, we want to share. I would just add a disclaimer stating "*since I'm not able to comment, posting this as an answer...*". – manisar Aug 27 '23 at 04:46
0

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
0

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")
redhatvicky
  • 1,912
  • 9
  • 8
  • THANKS! I can achieve the same goal if I use the Absolute Path to save the file. But I want to make it certain why this happen and how i can rewrite the settings to ensure every code file can run in the correct file path. – YANGSU LIU Apr 03 '19 at 11:33
  • one way to change the default directory to use for notebooks ,permanently is to change the config files. Firstly in the cmdline, type: $> ipython profile create to initialize a profile with the default configuration file. Secondly, in file ipython_notebook_config.py, uncomment and edit this line: c.NotebookApp.notebook_dir = 'Z:\\username_example\folder_that_you_whant' you can also follow the below approach : ipython notebook --notebook-dir=/path/to/specific/directory – redhatvicky Apr 03 '19 at 11:58