Skip to content

Instantly share code, notes, and snippets.

@GDBSD
Last active July 9, 2021 17:25
Show Gist options
  • Select an option

  • Save GDBSD/50d9436c1e925f9fd73d399d8da1fb1d to your computer and use it in GitHub Desktop.

Select an option

Save GDBSD/50d9436c1e925f9fd73d399d8da1fb1d to your computer and use it in GitHub Desktop.
jupter-easy-import-modules-from-higher-directories
import os
import sys
"""Utility function to update sys.path so in our notebooks you can import modules from
any folder in the application. It will also allow you to import any module in your virtual
environment. Note that in my project the virtual environment is named "venv".
In the notebook, in the first cell, import this script. It will run
automatically
import path_setter
"""
def set_path():
# You'll need to set abspath("../") according to your folder structure. In my project
# the notebooks folder is one level below the root.
project_path = os.path.abspath("../")
if project_path not in sys.path:
sys.path.insert(0, project_path)
sys.path.append(f'{project_path}/venv/lib/python3.7/site-packages')
print(f'{project_path} has been inserted into sys.path')
set_path()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment