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.
Import local function from a module housed in another directory with relative imports in Jupyter Notebook using Python 3 - Stack Overflow
How can I import code from the parent directory into my notebook?
How to import a .py file from a directory
How to import python module from a different directory into jupyter notebook - Stack Overflow
I had almost the same example as you in this notebook where I wanted to illustrate the usage of an adjacent module's function in a DRY manner.
My solution was to tell Python of that additional module import path by adding a snippet like this one to the notebook:
import os
import sys
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
sys.path.append(module_path)
This allows you to import the desired function from the module hierarchy:
from project1.lib.module import function
# use the function normally
function(...)
Note that it is necessary to add empty __init__.py files to project1/ and lib/ folders if you don't have them already.
I use this project structure:
my_project
βββ nb
β βββ 20170609-Examine_Database_Requirements.ipynb
β βββ 20170609-Initial_Database_Connection.ipynb
βββ my_package
βββ __init__.py
βββ postgres.py
And from a notebook:
In [1]: import os
os.chdir(os.path.expanduser("~/location_of/my_project")
In [2]: from my_package.postgres import database_connection
I am having the classic problem with my imports. I know that this is a very common issue to run into and I am still trying to understand it, but I could use a little help.
I have a package:
. βββ my_package β βββ main.py β βββ notebooks β β βββ notebook.ipynb β βββ utils.py βββ some_file.txt
The code is mostly in main.py, which imports utils.py for some helpers. But since I am also exploring certain things before properly developing in main.py, I have also a notebooks directory. Inside here I play around with a bunch of stuff and this requires access to the code from main, utils, etc.
My problem is that I cannot import those code. If I type, from my_package import utils I receive a ModuleNotFoundError: No module named 'my_package'. If I run a from .. import utils I receive ImportError: attempted relative import with no known parent package.
Am I correct in saying that the only proper way to do this is to make a proper setup with setup.py and $ pip install -e my_package? I do not like using sys to update the path, it seems dirty. If there is a way to do this without the setup then that would be great, as wasn't intending to make it a full package as it is only a small project and I am learning.
[edit] It seems that the "proper" way would be to go with the setup.py approach, but I just found a way to update the conda environment to include this permanently which is a decent solution. Just needed to type $ conda develop path/to/my_project and it creates a conda.pth file in the conda environment's site-packages directory.