There is no simple way to import python files in another directory. This is unrelated to the jupyter notebook
Here are 3 solutions to your problem
- You can add the directory containing the file you want to import to your path and then import the file like this:
import sys
sys.path.insert(1, '/path/to/application/app/folder')
import file
You can create a local module by having an empty
__init__.pyfile in the folder you want to import. There are some weird rules regarding the folder hierarchy that you have to take into consideration.You can create a module for the file you wish to import and install it globally.
There is no simple way to import python files in another directory. This is unrelated to the jupyter notebook
Here are 3 solutions to your problem
- You can add the directory containing the file you want to import to your path and then import the file like this:
import sys
sys.path.insert(1, '/path/to/application/app/folder')
import file
You can create a local module by having an empty
__init__.pyfile in the folder you want to import. There are some weird rules regarding the folder hierarchy that you have to take into consideration.You can create a module for the file you wish to import and install it globally.

Scenario: You have a folder named Jupyter containing your notebook, and you want to import a Python module (e.g., employee.py) located in a sibling folder named nn_webserver.
Folder structure
project_root
โโ Jupyter
โ โโ my_notebook.ipynb <- You're here
โโ nn_webserver
โโ employee.py <- Wish to import from here
Code
import os
import sys
# Build an absolute path from this notebook's parent directory
module_path = os.path.abspath(os.path.join('..', 'nn_webserver'))
# Add to sys.path if not already present
if module_path not in sys.path:
sys.path.append(module_path)
# Now you can import the desired function or class
from employee import motivation_to_work
Read more about imports here and here. Also explore linked question.
How to import a .py file from a directory
Importing local .py files into jupyter notebook?
python - Importing an ipynb file from another ipynb file? - Stack Overflow
How to import functions from a .py file into a .ipynb file?
ยป pip install importnb
This seems to be a very confusing topic for a lot of people, myself included, since I'm here asking about it.
I have some directory ../Project/utils/function.py
and I have another folder in ../Project/2021/my_notebook.ipynb
How do I import the function.py into the notebook? I know it has something to do with the path, but it's become very confusing for me. I just want to be able to create new projects in that 2021/ folder, and be able to import it using
from utils import function
This has to be possible, right? Is it something I can just "set and forget"? Or do I need to change the path for every new project?
It is really simple in newer Jupyter:
%run MyOtherNotebook.ipynb
See here for details.
Official docs: %run IPython magic command
Install my helper library from the command prompt:
pip install import-ipynb
Import it from your notebook:
import import_ipynb
Now import your .ipynb notebook as if it was a .py file
import TheOtherNotebook
This import-ipynb module is just one file and it strictly adheres to the official howto on the jupyter site.
PS It also supports things like from A import foo, from A import * etc
PPS Works with subdirectories: import A.B