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

  1. 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
  1. You can create a local module by having an empty __init__.py file in the folder you want to import. There are some weird rules regarding the folder hierarchy that you have to take into consideration.

  2. You can create a module for the file you wish to import and install it globally.

Answer from honeyspoon on Stack Overflow
🌐
Medium
skaf.medium.com β€Ί import-python-files-into-jupyter-notebook-while-being-in-another-directory-393e1bf76637
Import Python files into Jupyter Notebook while being in another directory.
December 9, 2023 - import os import sys directory_path = os.path.abspath(os.path.join('..')) if directory_path not in sys.path: sys.path.append(directory_path) This code will add the path specified in this statement os.path.abspath(os.path.join(β€˜..’)) to the ...
Discussions

Import local function from a module housed in another directory with relative imports in Jupyter Notebook using Python 3 - Stack Overflow
I have a directory structure similar to the following meta_project project1 __init__.py lib module.py __init__.py notebook_folder notebook.j... More on stackoverflow.com
🌐 stackoverflow.com
How can I import code from the parent directory into my notebook?
Are you able to print the working directory? os.getcwd() Perhaps that might offer a clue as to where the notebook is running. Are you in an IDE that is setting the sources directory? More on reddit.com
🌐 r/learnpython
15
3
October 14, 2022
How to import a .py file from a directory
Did you try the following? import test_standard More on discourse.jupyter.org
🌐 discourse.jupyter.org
2
0
March 20, 2021
How to import python module from a different directory into jupyter notebook - Stack Overflow
Importing from python modules inside parent directory into jupyter notebook files inside subdirectory More on stackoverflow.com
🌐 stackoverflow.com
🌐
Jupyter Community Forum
discourse.jupyter.org β€Ί meta
How to import a .py file from a directory - Meta - Jupyter Community Forum
March 20, 2021 - I am trying to import a .py file into my jupyter notebook. using the following code , but it is throwing an error. I have also included init.py file in the directory import sys import os sys.path.append(os.path.abspath("/Users/hp/Documents/AFR")) import test_standard.py output No module named ...
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί how can i import code from the parent directory into my notebook?
r/learnpython on Reddit: How can I import code from the parent directory into my notebook?
October 14, 2022 -

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.

🌐
Jupyter Community Forum
discourse.jupyter.org β€Ί meta
How to import a .py file from a directory - #2 by fomightez - Meta - Jupyter Community Forum
March 20, 2021 - I am trying to import a .py file into my jupyter notebook. using the following code , but it is throwing an error. I have also included init.py file in the directory import sys import os sys.path.append(os.path.abspath…
Find elsewhere
🌐
Stack Overflow
stackoverflow.com β€Ί questions β€Ί 64204057 β€Ί how-to-import-python-module-from-a-different-directory-into-jupyter-notebook
How to import python module from a different directory into jupyter notebook - Stack Overflow
I wanted to import my_module.py in my_notebook.ipynb, so I first tried import src.my_module getting ModuleNotFoundError: No module named 'src' and from ..src import my_module getting ImportError: attempted relative import with no known parent package.
🌐
Saturn Cloud
saturncloud.io β€Ί blog β€Ί how-to-import-python-file-as-module-in-jupyter-notebook
How to Import Python File as Module in Jupyter Notebook | Saturn Cloud Blog
May 1, 2026 - This is important because Python ... a module in your Jupyter notebook, you need to use the import keyword followed by the name of your Python file without the .py extension....
🌐
GitHub
gist.github.com β€Ί jkarakas β€Ί e8f1ec281fe7b8bc92650973fd44bda4
Import(run) a ipynb file from another folder in a jupyter notebook Β· GitHub
Import(run) a ipynb file from another folder in a jupyter notebook - Import(run) a ipynb file from another folder in a jupyter notebook.ipynb
🌐
Stack Overflow
stackoverflow.com β€Ί questions β€Ί 68494514 β€Ί import-files-from-different-directory-in-jupyter-notebook
python - import files from different directory in jupyter notebook - Stack Overflow
July 23, 2021 - This is normally what I would do to import a function from an external ipynb file in the same directory, from ipynb.fs.full.my_functions import split_into_sentences Β· I've moved my_functions into functions/ and I've tried this, from ipynb.fs.full."functions/my_functions" import split_into_sentences and from ipynb.fs.full.functions/my_functions import split_into_sentences Β· Neither works. ... Importing from python modules inside parent directory into jupyter notebook files inside subdirectory
🌐
YouTube
youtube.com β€Ί watch
How to Import Python Module Files into Jupyter Notebooks - YouTube
How to Import python files into jupyter notebooksGet FREE Tutorials, Free Books, plus other FREE Resources - https://evidencenmedia.com/freeFollow me on twit...
Published: September 13, 2022
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί how to import from imported .py in subdirectory (from jupyter notebook)
r/learnpython on Reddit: How to import from imported .py in subdirectory (from jupyter notebook)
July 13, 2019 - tl;dr: How to import python files inside subdirectory from jupyter notebook? As expected, I were going in the wrong direction. Using chdir to set subdirectory and then run the subdir files solved everything without the need to modify subdir files.
🌐
Medium
medium.com β€Ί @pablosjv β€Ί importing-local-python-modules-into-jupyter-notebooks-87336b2d7746
Importing local Python modules into Jupyter Notebooks | Medium
November 25, 2021 - This is an extension that is installed by default with Jupyter. It allow us (as its name suggest) to autoreload the imported modules when there is any changes. Otherwise, you will need to restart the kernel. The second part is to add the path to your source code to your PYTHONPATH so it can be imported.
🌐
Readthedocs
mg.readthedocs.io β€Ί importing-local-python-modules-from-jupyter-notebooks β€Ί index.html
Importing Local Python Modules from Jupyter Notebooks β€” homepage
July 26, 2019 - The following sections are created from Jupyter notebooks which show multiple ways to import local Python modules, even if they are located in sub-directories. The file module-subdirectory/mymodule.py is used as a dummy example module.
🌐
Jupyter Notebook
jupyter-notebook.readthedocs.io β€Ί en β€Ί stable β€Ί examples β€Ί Notebook β€Ί Importing Notebooks.html
Importing Jupyter Notebooks as Modules β€” Jupyter Notebook 7.6.3 documentation
It is a common problem that people want to import code from Jupyter Notebooks. This is made difficult by the fact that Notebooks are not plain Python files, and thus cannot be imported by the regular Python machinery.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python-import-module-from-different-directory
Python – Import module from different directory - GeeksforGeeks
June 20, 2024 - # importing module1 from another folder import Folder_2 # calling odd_even function module1.odd_even(5) ... ModuleNotFoundError, because by default Python interpreter will check for the file in the current directory only, and we need to set the file path manually to import the modules from another directory.
🌐
GitHub
github.com β€Ί jupyter β€Ί notebook β€Ί issues β€Ί 3281
ImportError when import module in the same folder Β· Issue #3281 Β· jupyter/notebook
January 31, 2018 - Hi, I meet a trouble that is very strange, I want to import a module called data_prep, and the data_prep code was put in the same folder with the code that I run. The data_prep code works fine. But when I want to import this code, it giv...
Author: jupyter
🌐
Stack Overflow
stackoverflow.com β€Ί questions β€Ί 56350330 β€Ί how-can-i-run-py-file-from-another-directory-in-jupyter-notebook
How can I run .py file from another directory in jupyter notebook? - Stack Overflow
May 29, 2019 - I am working on a jupyter notebook file in a folder project. I have a package_code.py file in another folder called packages. How can I run the package_code.py file in the jupyter notebook file I am