I know there are many questions on this topic but none of them helped me much.
I have a python project directory i.e. git_project
(git repository). What I want is to create a separate directory called notebooks
where I will keep all my notebooks for analysis using git_project. I don't want to put notebooks within the root of git_project. I have kept both git_project
and notebooks
directory in a general directory where keep all of my projects. I have the following structure:
my_projects
│
├── notebooks
│ └── notebook.ipynb
└── git_project
└── config
└── cfg.json
└── source
└── config.py
The contents of config.py
:
import json
def get_cfg():
with open('config/cfg.json', 'r') as f:
cfg = json.load(f)
return cfg
Contents of the notebook.ipynb
:
import sys
import os
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
sys.path.append(module_path)
from git_project.source.config import get_cfg
get_cfg()
Now when I run the code in notebook.ipynb I get the following error:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-7-6796ee7f0100> in <module>
----> 1 get_cfg()
~/Documents/my_projects/git_project/source/config.py in get_cfg()
1 def get_cfg():
----> 2 with open('config/cfg.json', 'r') as f:
3 cfg = json.load(f)
4 return cfg
FileNotFoundError: [Errno 2] No such file or directory: 'config/cfg.json'
However, If I move the notebook.ipynb
file to the root of git_project. Then I do not get this error. This is just one example. I have so many similiar problems in other modules of git_project and git_project contains the code which is already running in the production environment. So changing anything in git_project is not feasible here.
But as I said I do not want to move the notebooks inside of git_project but rather like to keep them in a parallel directory for analysis purposes. I can provide more information, if required.
I am using Python 3.6+ which does not even require to put init.py file anymore to make a directory package.
What should I do in order for this to work? Any help will be much appreciated.