Prevent Jupyter Notebooks from changing cwd

My folder structure looks like this:

- project/
  - notebooks/
    - notebook1.ipynb
  - src/
    - module1.py
    - __init__.py
  - data/
    - data.csv

      

This way I can separate the source code from the actual analysis. I would like to be able to import modules from src

and use them in notebook1

, however whenever I open notebook1

, Jupyter decides to change my working directory inside notebooks

.

This makes it difficult to keep my imports as I have to import things on a whim of Jupyter - is there a way to fix cwd

it so that it always stays project

no matter what file I have open

I looked through the docs and my file ~/.jupyter/jupyter_notebook_config.py

but couldn't find anything that could help me.

EDIT: I would not like to use os.chdir

or cd

at the top of every script.

Thanks for any help

+3


source to share


1 answer


First of all, I think you mean cwd

, pwd

- reduction for print working directory

while cwd

is short for current working directory

. Essentially it pwd

prints cwd

. Just a little terminology problem there!

Second, you can always manually change the directory yourself at the top of the laptop:



import os
os.chdir("../")  # or manually specify project - whichever you prefer

      

I don't think there is any way to change the default behavior automatically - perhaps opening the issue on a notebook git repo would be a good idea? https://github.com/jupyter/notebook

0


source







All Articles