0

I really like to template my projects using this structure:

├── notebooks          
│   └── 00_my_analysis.ipynb   
├── src                
│   └── my_module
│       └──my_function.py
└── ... my_other_files

To import (from src.my_module import my_function) from notebook notebooks\00_my_analysis.ipynb I have to insert into the first cell the following snippet:

import os
if os.getcwd().endswith('notebooks'):
    os.chdir('..')
    print(f"change path to project parent folder {os.getcwd()}")
else:
    print("already in the parent project folder")

In PyCharm, where I usually write most of my code, is it possible to specify a working directory which is different from the path in which the file is located (see "Script Path" and "Working Directory" parameters into the docs).

Is there any configuration option that I am missing which could let me obtain the same result and avoid include the code above every time in the first cell?

I have tried several suggestion (such as in this or several others) related to changeing notebook working directory, but they are changing the notebook (and the viewed file browser in jupyter lab) default directory, not the python working directory.

Other related questions (such as this) solve this problem with the same workaround I am already using, while I am interested in a less repetitive/manual solution.

ndricca
  • 450
  • 4
  • 11

0 Answers0