8

I'm trying to set current working directory (CWD) to edited file location for Jupyter Notebook in VS Code. I use ${fileDirname} in python.dataScience.notebookFileRoot setting. However it uses temporary folder as ${fileDirname} instead of original file folder.

Same issue was discussed couple times already (e.g. https://stackoverflow.com/a/54794976/12488601) with tried solution pointed out.

Here is example of cwd:

os.getcwd()
.. 'C:\\Users\\MjH\\AppData\\Local\\Temp\\1f6cc207-562f-4ae1-8754-e2013ae2c12d'

While expected result is C:\Workspace\Project.

So use of ${fileDirname} does not work in my case. I use following ad-hoc solution, which, obviously, won't update if file is moved.

import sys
import os
sys.path.insert(0, r'C:\workspace\project')
os.chdir(sys.path[0])

Now I'm trying to understand three things:

  1. is my case a unique one?
  2. if it's general issue, is there a feature request/issue submitted for VS Code to address it?
  3. is there a better ad-hoc solution?

VS Code version: Code 1.40.2 (f359dd6, 2019-11-25T14:54:45.096Z)
OS version: Windows_NT x64 10.0.17763 Settings

MjH
  • 1,170
  • 1
  • 8
  • 16
  • MjH. I've not seen anyone else reporting this. When you start up a notebook could you post the contents of your developer console log? It's under Help->Developer tools. I should be able to see where it's trying to set the working directory in there. – Ian Huff Dec 16 '19 at 23:20
  • Ian Huff, Here is line 412 where temp folder first appears: workbench.desktop.main.js:sourcemap:254 [Extension Host] Info Python Extension: 2019-12-17 20:33:50: Generating custom default config at C:\Users\MjH\AppData\Local\Temp\7decbe34-3c30-447a-94d9-3aeb9a0bbe48\jupyter_notebook_config.py. Full log is here: https://pastebin.com/6x2WxK8D – MjH Dec 17 '19 at 09:42
  • 1
    Thanks I'm looking over the log now. Just to check, was it typed in exactly as: ${fileDirname} without quotes in the settings? I know that at some point some folks were typing in ${fileDirName} or "${fileDirname}" both of which would have an issue. Off hand that's the only think that I'm currently coming up with. If we fail to resolve the variable we default back to the location of the temporary ipynb file that we open, which is what you are seeing. – Ian Huff Dec 17 '19 at 17:26
  • It actually was ${fileDirName} with capital N. I changed it and restarted VSC but still end up with temp dir. Added snapshot of settings to the question. – MjH Dec 17 '19 at 20:23

5 Answers5

23

As of January 2021 adding the following line in my setting helps to solve the issue

"jupyter.notebookFileRoot": "${workspaceFolder}",
Espoir Murhabazi
  • 5,973
  • 5
  • 42
  • 73
8

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!

Edit :

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

Machavity
  • 30,841
  • 27
  • 92
  • 100
CristoJV
  • 490
  • 6
  • 15
  • Thank you first of all. I gave it a try and it did not work. I'm aware about workspaceFolder option. I don't have big projects, I have small experimental stuff, so I don't want to change workspaceFolder every time. – MjH Jan 18 '20 at 09:14
  • This solution worked for me. I had gone through many other options to amend the PYTHONPATH system variable so that my path was correct...no luck. All of the PYTHONPATH options stopped working when I introduced virtual environments to my project. Telling VS Code to use the workspace folder the analysis starting point is genius. Kudos! – Eric D. Jan 20 '20 at 20:52
0

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])
Anteino
  • 1,044
  • 7
  • 28
  • Thanks for suggestion, but not gonna work. My CWD is temp folder. So above would get temp folder address with 'cd' (and write it to 'dir' file). – MjH Dec 06 '19 at 02:31
0

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.

Espoir Murhabazi
  • 5,973
  • 5
  • 42
  • 73
  • Sorry, haven't come back to that topic for a while since shifting to C#. Will give a try. thank you for the time spend posting it here! – MjH Sep 17 '20 at 14:01
0

This is true solution to this problem. Worked in my version of VS 1.76.2; JupyterEx v2023.2.1200692131

In user settings json file put this

{
"jupyter.runStartupCommands": [
        "import os",
        "__t=os.path.dirname(__vsc_ipynb_file__)",
        "%cd {__t}",
        "del __t"
    ],
}

It is using undocumented variable - so this might break in the future - but works for now.

THOTH
  • 109
  • 1
  • 12