Jupyter Notebook default working directory

Answered

When running Jupyter notebooks, the default working directory is the directory the notebook is open in. Problem is that when running a Jupyter notebook inside of IDEA / PyCharm, the default working directory is the project root.

Is there a way to make PyCharm's python notebook and vanilla Jupyter notebook behave the same? It's a pain to add conditional os.chdir(...) to make notebooks that open files work in both PyCharm and off the notebook server UI.

In the screenshots below, the notebook is in ~/Code/datalab/examples/path.ipynb

IntelliJ IDEA 2017.2.3
Build #IU-172.3968.16, built on August 21, 2017
JRE: 1.8.0_152-release-915-b11 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Mac OS X 10.12.6
Python Add-In: 2017.2.172.3757.70

8
19 comments

You can specify the worjing deirectory in your Run configuration (Run| Editc onfigurations), please see: https://www.jetbrains.com/help/pycharm/run-debug-configuration-jupyter-notebook.html.

-6

Can you specify that the working directory should be the same directory as the notebook file itself? By default in Jupyter if I have:

project_root/myproject.iml

project_root/folder1/mynotebook1.ipynb

project_root/foder2/mynotebook2.ipynb

The working directory for mynotebook1 is folder1. For mynotebook2 it's folder2. The problem with PyCharm is that the working directory project_root. By changing the setting you suggested I can set the working directory to be folder1, but that would also change mynotebook2's working directory to it.

Right now to make my workbooks shareable, my workaround is to begin my notebook's with a path check and call os.chdir which seems like a hack.

4
Avatar
Permanently deleted user

Anna, the working dir configuration is not working as expected. It sets the workdir for the whole jupyter server.

When running a notebook from a standalone server, every notebook's path is relative to the notebook file itself.

If I start jupyter from PyCharm and then open an external browser an open a notebook there, the external browser will still report the working dir relative to the notebook file. And it's different from what PyCharm is considering to be a workdir.

In short, the workdir must follow the path of the jupyter notebook, it is set for every notebook opened, but PyCharm only sets it once (incorrect).

3
Avatar
Permanently deleted user

Bump:

Is there any workaround here? it is rather annoying!

3
Avatar
Permanently deleted user

It would be great if the PyCharm default was to use the notebook file's dir like a standalone server does.

My current workaround is to have a separate PyCharm configuration for each dir I want to run notebooks out of.

0
Avatar
Permanently deleted user

EDIT: This was posted in error.  The run configuration approach works. 

Original post: The workaround specified in the top-level response is no longer valid.  Are we stuck with the path check and call os.chdir() call?

-1

Hi Aju,

Why isn't it valid? There is a working directory in Run/Debug Configuration.

-1
Avatar
Permanently deleted user

Hello, I saw that pycharm updated so I checked in on the functionality around jupyter. This is still a problem in pycharm 2019.2. That you can override the project wide working directory in Run/Debug Configuration is not particularly useful since the original jupyter notebook behavior is per file, this is well demonstrated in the original post (note that the current working directory is different for the same notebook being run in browser vs pycharm). In any jupyter project with notebooks in more than one directory (which is obviously common among non-trivial projects) overriding the working directory for the whole project is incorrect. In order to be useful, this behavior must be the same as the default jupyter notebook (current working directory = directory of the notebook). This and the reduced hotkey/shortcut functionality in pycharm vs the browser notebook are the 2 things keeping my whole team from using pycharm for jupyter work. Having to know the location of the file you are working in within the file itself and updating it if the file moves is not an acceptable trade off.

If you are serious about making pycharm a client for jupyter servers and working with jupyter notebooks, I think you need to spend more time looking at real use cases for jupyter.

6

Hey Marr75,

This is what I do for my notebooks so they work fine in PyCharm and Jupyter.

import sys

if '..' not in sys.path: # My notebooks are usually one directory in.

    sys.path.append('..')

 

-2

The behavior doesn't seem to be reproducible on 2019.2 any more.

Moving .ipynb file to another directory/sub-directory and the calling `os.getcwd()` returns this directory.

-3

The option to configure a working directory that was proposed seem no longer to be present.

0

Since PyCharm 2019.x the new implementation of Jupyter is not using run/debug configurations, so working directory settings have no effect.

I'm testing in PyCharm 2019.3 and behavior seems consistent with native Jupyter web app. While working in PyCharm, the working directory returned by `%pwd` command is always the directory where .ipynb file is opened it. 

If this is not the case for you, please provide steps to reproduce the issue, as it is not easily reproducible in my case.

2

Everything is fine now except PyCharm/IntelliJ randomly freezes in macOS and needs a force quit. There's a couple issues logged for this:

https://youtrack.jetbrains.com/issue/JBR-1609

https://youtrack.jetbrains.com/issue/JBR-1816

https://youtrack.jetbrains.com/issue/JBR-1628

 

0
Avatar
Permanently deleted user

Unfortunately, the problem persists in PyCharm 2019.3 for remote Jupyter servers. A notebook is still launched in the root directory of the Jupyter server and not in its own directory.

5
Avatar
Permanently deleted user

No proposed solution works in the last versions.

 

Support was removed here: https://youtrack.jetbrains.com/issue/PY-32934

Adding it back is being considered here: https://youtrack.jetbrains.com/issue/PY-35329 . Upvote if you're interested!

1
Avatar
Permanently deleted user

Unfortunately, the problem persists in PyCharm 2019.3 for remote Jupyter servers.

This! I have Windows 10, PyCharm 2021.3.1 and I'm running a Jupyter server on WSL. Executing a Jupyter notebook uses /home/myusername as the default working directory, not the directory where the notebook file is located.

Adding it back is being considered here: https://youtrack.jetbrains.com/issue/PY-35329 . Upvote if you're interested!

Too bad but this link does not work.

2

Hello,

Using PyCharm 2022.3.1 and problem persist however only in case of the remote Jupyter server. If you switch to server managed by PyCharm your working directory will be set as expected - to the directory where your Jupyter notebook file is saved.

0

I am having this issue when using a “Configured Server” in PyCharm 2023.3.4. Is there any workaround that doesn't involve adding extra code to all my notebooks?

2

For the PyCharm managed server, you can click it in the upper right and configure it to have command line arg `--notebook-dir=` for the directory the notebook is in, or add your own working directory.

0

Please sign in to leave a comment.