How to set python path using jupyter

Viewed 599

My project structure is as follows

project_name/
    project_name/
        __init__.py
        sub_package1/
            __init__.py
            some_module1.py
        sub_package2/
            __init__.py
            some_module2.py
    scripts/
        some_script.py
    notebooks/
        some_notebook.py
    top level files <requirements.txt, .gitignore, README.md, ...>

Now I want to be able in my python scripts inside the scripts folder and in my notebooks inside the notebooks folder to access my main library as

from project_name.sub_package1 import some_module1

What is the best way to achieve this?
Some ideas I came up with:

1)

In my notebooks folder, create a file context.py with the content

 sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

Then, in my notebooks I can do

from context import project_name  

and work from there on. This is ok-ish but of course it would be nicer to be able to directly import project_name without the context in my notebook.

2)

In the notebook execute

notebook_dir = os.path.split(os.getcwd())[0]
if notebook_dir not in sys.path:
    sys.path.insert(0, notebook_dir)

since dunder file does not exist in an interactive shell. Here, I am not entirely sure what happens if I change the cwd somewhere and after all having this clutter in the beginning of my notebook is somewhat undesirable.

3)

Add the project_name root folder to the python path. If this can be done in a non-hardcoded way, this would be great. I use pyenv and pyenv virtualenv.

Thank you for your suggestions, Matt

0 Answers
Related