简体   繁体   中英

Prevent Jupyter Notebooks from changing the cwd

My folder structure is set out like this:

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

Such that I can separate my 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 to be inside notebooks .

This makes my imports harder to maintain as I'd have to import things relatively to Jupyter's whim - is there any way I can fix the cwd such that it is always project , no matter what file I have open?

I have looked through the docs and my ~/.jupyter/jupyter_notebook_config.py , file but have found nothing that could help me.

EDIT: I'd like to not have to use os.chdir or cd at the top of every script.

Thanks for any help

First of all, I think you mean cwd , pwd is the shorthand for print working directory , whereas cwd is the shorthand for current working directory . Essentially, the pwd prints the cwd . Just a minor terminology issue there!

Secondly, you could always manually change the directory yourself at the top of the notebook:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM