Vscode-jupyter: Jupyter notebook support not finding the correct cwd

Created on 25 Apr 2019  Â·  11Comments  Â·  Source: microsoft/vscode-jupyter

Environment data

  • VS Code version: 1.33.1 (Tried with both anaconda installation and debian package installation)
  • Extension version: 2019.4.11987
  • OS and version: Ubuntu 18.04 LTS
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7.3 (Anaconda)
  • Type of virtual environment used: conda

Expected behavior

Find the actual path of my python file (/home/theis/code/N_path_intership/PlottingCode/python)

Actual behavior

Debugger does it correctly after adding "cwd": "${fileDirname}" to launch.json, jupyter support doesn't.

Steps to reproduce:

  1. Simply run
#%%

import os
print("Hello World!")
print(os.getcwd())

GIF version

Without "cwd": "${fileDirname}"

alt-text

With "cwd": "${fileDirname}"

alt-text

Code of my .json files

launch.json
settings.json

Most helpful comment

I have the same issue. ${fileDirname} is the default value of python.dataScience.notebookFileRoot but it is completely useless for the remote jupyter server (even if the remote Jupyter server is actually local).

vscode remote jupyter wrong cwd

I noticed that some Untitled*.ipynb were created in the default working directory of the remote jupyter server. So if VSCode is running code in these new notebook files, the working directory will obviously not be correct.

However, I have to use a remote Jupyter server because I need to load pyspark and I haven't found out how to set up a jupyter startup command in VSCode.

I actually have to start jupyter with the following command:

PYSPARK_DRIVER_PYTHON=jupyter PYSPARK_DRIVER_PYTHON_OPTS="notebook --no-browser" PYSPARK_PYTHON=python3 pyspark

In the end, I chose to first cd to the directory I wanted and then start the remote Jupyter server. This solves the problem, but is not very convenient.

All 11 comments

Currently we don't support reading variables from the launch.json or settings.json file. To work around you can set the Data Science: Notebook File Root. However, this only supports hardcoded paths (with the single exception of ${workspaceFolder}.

Thank you for tour answer! Looking forward to when you guys add this feature.

@guilhermetheis the notebook file root now supports all the variables listed here:
https://code.visualstudio.com/docs/editor/variables-reference

That might solve the problem you have with launch.json.

This doesn't work for me, even though the related microsoft/vscode-python#4441 is now closed. By "not working," I mean that os.curdir is still pointing to a temp dir instead of the notebook's directory. This is with a fresh reinstall of VSC on 12/11/19.

This doesn't work for me

Are you talking about the 'python.dataScience.notebookFileRoot'? Did you set it to ${fileDirname}?

Yes, I set python.dataScience.notebookFileRoot to ${fileDirname}.

And you restarted VS code afterwards? That value is sent when we initially connect to the server.

For example, if you restart vscode and run this from a cell:

import os
os.getcwd()

it should point to the directory of the file you ran the cell from. If it doesn't, can you tell us the version of the python extension you have installed, and upload your Developer Tools Console log?

Thanks.

Hi Rich,

Thanks for the response. Yes, I restarted it, and os.getcwd() doesn't
print the director the script is in.

For example, if I

  1. Put this code:
    import os
    print(f"cwd={os.getcwd()}")
    in
    c:\Users\Scott\OneDrive - Clean Power
    Research\Research\lib\python\test\sandbox\vscode_init_dir_test.py

  2. Open vscode_init_dir_test.py in a freshly started VScode

  3. In VSCode, click Debug->Start Debugging

  4. Then finally, in VSCode, click the little "run triangle" at the bottom
    of the VSCode window

Then VSCode terminal shows the following:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

Loading personal and system profiles took 605ms.
(base) PS C:\Users\Scott\Research\lib\python> conda activate stdso
(stdso) PS C:\Users\Scott\Research\lib\python> &
'C:\Users\Scott\Anaconda3\envs\stdso\python.exe'
'c:\Users\Scott.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\ptvsd_launcher.py'
'--default' '--client' '--host' 'localhost' '--port' '58663'
'c:\Users\Scott\Research\lib\python\test\sandbox\vscode_init_dir_test.py'

cwd=C:\Users\Scott\Research\lib\python
(stdso) PS C:\Users\Scott\Research\lib\python> cd
'c:\Users\Scott\Research\lib\python'; ${env:PYTHONIOENCODING}='UTF-8';
${env:PYTHONUNBUFFERED}='1'; &
'C:\Users\Scott\Anaconda3\envs\stdso\python.exe'
'c:\Users\Scott.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\ptvsd_launcher.py'
'--default' '--client' '--host' 'localhost' '--port' '58687'
'c:\Users\Scott\Research\lib\python\test\sandbox\vscode_init_dir_test.py'
cwd=C:\Users\Scott\Research\lib\python
(stdso) PS C:\Users\Scott\Research\lib\python>

For some reason, CWD is two directories above the location of the script.
Other times, CWD has been in some TEMP directory, but I don't remember how
I started everything that time.

Scott

On Thu, Dec 12, 2019 at 9:21 AM Rich Chiodo notifications@github.com
wrote:

And you restarted VS code afterwards? That value is sent when we initially
connect to the server.

For example, if you restart vscode and run this from a cell:

import os
os.getcwd()

it should point to the directory of the file you ran the cell from. If it
doesn't, can you tell us the version of the python extension you have
installed, and upload your Developer Tools Console log?

Thanks.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/microsoft/vscode-python/issues/5468?email_source=notifications&email_token=AA5MM43MWRFHSG5MJSYVMRTQYJXKZA5CNFSM4HIKXSPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGXMPMQ#issuecomment-565102514,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AA5MM4ZEOLWGVZBQCWT7IATQYJXKZANCNFSM4HIKXSPA
.

@notuntoward it seems that you're running from the terminal. The terminal defaults to the workspace directory (hence your cwd being two levels above), whereas a notebook uses the python.dataScience.notebookFileRoot value to compute the start directory.

I have the same issue. ${fileDirname} is the default value of python.dataScience.notebookFileRoot but it is completely useless for the remote jupyter server (even if the remote Jupyter server is actually local).

vscode remote jupyter wrong cwd

I noticed that some Untitled*.ipynb were created in the default working directory of the remote jupyter server. So if VSCode is running code in these new notebook files, the working directory will obviously not be correct.

However, I have to use a remote Jupyter server because I need to load pyspark and I haven't found out how to set up a jupyter startup command in VSCode.

I actually have to start jupyter with the following command:

PYSPARK_DRIVER_PYTHON=jupyter PYSPARK_DRIVER_PYTHON_OPTS="notebook --no-browser" PYSPARK_PYTHON=python3 pyspark

In the end, I chose to first cd to the directory I wanted and then start the remote Jupyter server. This solves the problem, but is not very convenient.

This should be solved when microsoft/vscode-jupyter#827 is fixed

Was this page helpful?
0 / 5 - 0 ratings