You can add a path using sys to your local module/python file.

import sys
sys.path.append("/path/to/file/")  # path contains python_file.py

import python_file

If you want a more permanent solution by adding module to Anaconda path, see previous answer from cord-kaldemeyer https://stackoverflow.com/a/37008663/7019148. Content copied below for completeness:

I found two answers to my question in the Anaconda forum:

1.) Put the modules into into site-packages, i.e. the directory $HOME/path/to/anaconda/lib/pythonX.X/site-packages which is always on sys.path. This should also work by creating a symbolic link.

2.) Add a .pth file to the directory $HOME/path/to/anaconda/lib/pythonX.X/site-packages. This can be named anything (it just must end with .pth). A .pth file is just a newline-separated listing of the full path-names of directories that will be added to your path on Python startup.

Both work straightforward and I went for the second option as it is more flexible.

*** UPDATE:

3.) Create a setup.py in the folder of your package and install it using pip install -e /path/to/package which is the cleanest option from my point of view because you can also see all installations using pip list.

Thanks anyway!

Answer from tda on Stack Overflow
Top answer
1 of 3
9

You can add a path using sys to your local module/python file.

import sys
sys.path.append("/path/to/file/")  # path contains python_file.py

import python_file

If you want a more permanent solution by adding module to Anaconda path, see previous answer from cord-kaldemeyer https://stackoverflow.com/a/37008663/7019148. Content copied below for completeness:

I found two answers to my question in the Anaconda forum:

1.) Put the modules into into site-packages, i.e. the directory $HOME/path/to/anaconda/lib/pythonX.X/site-packages which is always on sys.path. This should also work by creating a symbolic link.

2.) Add a .pth file to the directory $HOME/path/to/anaconda/lib/pythonX.X/site-packages. This can be named anything (it just must end with .pth). A .pth file is just a newline-separated listing of the full path-names of directories that will be added to your path on Python startup.

Both work straightforward and I went for the second option as it is more flexible.

*** UPDATE:

3.) Create a setup.py in the folder of your package and install it using pip install -e /path/to/package which is the cleanest option from my point of view because you can also see all installations using pip list.

Thanks anyway!

2 of 3
3

look, on python 2.7 it will be work, but on python 3 you get some errors...

if you write some functions in other file, you need import this file. if this file in the same folder - it is good. but if in sub folder... in sub folder you need create empty file init.py
and now, if you will try import your module from sub file - also will be work

impotent (for python 3):
use this code

import os
import sys

sys.path.append(os.getcwd() + '/modules')
import my_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.
Discussions

Jupyter Notebook will not import module
To investigate which interpreter your notebook is using, run this in the first cell: !which python; python -V; This will tell you if you are running a different python then where you installed your libraries. Once you are more familiar with python, I would highly suggest using either a virtual environment or anaconda to manage your packages and interpreters. Makes things so much easier when dealing with multiple projects that might be using different versions of the same package. More on reddit.com
🌐 r/learnpython
4
8
October 4, 2018
python - Module not found during import in Jupyter Notebook - Stack Overflow
But it has few disadvantages that are found rather empirically then through the documentation. For example, you cannot write something like import .module1. I've found this exception to be raised even if import MyPackage is ran from the usual Python console. Not from IPython or Jupyter Notebook. More on stackoverflow.com
🌐 stackoverflow.com
Python in terminal finds module, jupyter notebook does not
Hi, I am in a situation where python, in a terminal, finds a module with “import”. In fact, with “print(help(‘modules’)” I see a list of all available modules, and the module in question is listed. However, when running a jupyter notebook, of the proper version of python, I always ... More on discourse.jupyter.org
🌐 discourse.jupyter.org
11
0
September 29, 2019
Installed package won't import in notebook
I'm using the notebook from a conda env, and have the environment set up with all of my required packages installed. However, when I try to import one of these (seaborn) in the notebook, I get ... More on github.com
🌐 github.com
96
April 2, 2017
🌐
Saturn Cloud
saturncloud.io › blog › how-to-fix-import-errors-in-jupyter-notebook
How to fix import errors in Jupyter Notebook | Saturn Cloud Blog
May 1, 2026 - For example, if you are trying ... Notebook uses the same Python environment as the .py file, you can activate the environment in Jupyter Notebook....
🌐
Jupyter Community Forum
discourse.jupyter.org › notebook
Python in terminal finds module, jupyter notebook does not - Notebook - Jupyter Community Forum
September 29, 2019 - Hi, I am in a situation where python, in a terminal, finds a module with “import”. In fact, with “print(help(‘modules’)” I see a list of all available modules, and the module in question is listed. However, when running a jupyter notebook, of the proper version of python, I always ...
🌐
HopHR
hophr.com › tutorial-page › getting-import-error-jupyter-notebook-but-not-python-step-by-step-guide
Why am I getting an import error in Jupyter Notebook but not in Python? | HopHR
Step 4: Compare the Two Environments Compare the two paths. If they're different, this means that Jupyter and your terminal are using different Python environments. This could be why you're able to import the module in Python (in your terminal) but not in Jupyter.
Find elsewhere
🌐
GitHub
github.com › jupyter › notebook › issues › 2359
Installed package won't import in notebook · Issue #2359 · jupyter/notebook
April 2, 2017 - --------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-2-09fdb2483669> in <module>() ----> 1 import notebook 2 3 notebook.__version__ ImportError: No module named 'notebook'
Author: jupyter
🌐
Jupyter Notebook
jupyter-notebook.readthedocs.io › en › 4.x › examples › Notebook › rstversions › Importing Notebooks.html
Importing Jupyter Notebooks as Modules — Jupyter Notebook 4.4.1 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.
🌐
Copernicus
forum.dataspace.copernicus.eu › jupyterlab
Cannot import python function in notebook? - JupyterLab - Copernicus Data Space Ecosystem | Community forum
November 19, 2024 - I try to import a function from an existing python script into my notebook for re-use. This works fine in my locally hosted JupyterLab, but not on CDSE JupyterLab. Any reason for this? Both the notebook and the python script are stored in the same directory, as required (mystorage) Trivial example: Notebook: from stupid import stupid stupid(5) python script (stupid.py): def stupid(x): print(f"{x}!") ModuleNotFoundError: No module named ‘stupid’
Top answer
1 of 6
14

I had the same issue. After going through many (like way too many) solutions to this issue found elsewhere, I manage to figure out a solution that at least works in my case.

Step1: check the correct executable path of the anaconda environment.

Go on command line, activate the conda environment that is problematic, and check the correct executable path for the environment.

conda activate {envronment name};
then on python console, (>>>)import sys;sys.executable

For instance on Linux it will be /media/{username}/{path-to}/anaconda3/envs/{environment name}/bin/python

Step2: correct the executable path for jupyter sessions.

From command line, check the path where kernel.json of your problematic conda environment is located.

jupyter kernelspec list

For instance on Linux it will be: /home/{username}/.local/share/jupyter/kernels/{environment name}

Open the kernel.json located in that folder and replace the incorrect executable path, as shown below.

{
 "argv": [
  "REPLACE-THIS-WITH-THE-CORRECT-EXECUTABLE-PATH",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "environment name",
 "language": "python"
}
 

Hope this works in your case too.

2 of 6
10
  1. Open a new terminal window and see if this helps. If not, proceed with 2.

  2. Start a standard Python session from the terminal and type this:

    >>> import sys
    >>> sys.executable
    
  3. Do the same in the notebook:

    In [1]: import sys
            sys.executable
    
  4. Compare the results. Hopefully, this gives you a clue what is going on.

🌐
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 ‘test_standard.py’; ‘test_standard’ is not a package
🌐
DEV Community
dev.to › moyarich › fix-python-imports-in-jupyter-notebooks-3kp0
Fix Python Imports in Jupyter Notebooks - DEV Community
April 28, 2026 - try: with RelativeImportContext("./app") as ctx: import broken_module except Exception as e: print("Error:", e) print(ctx.get_logs()) ... While RelativeImportContext is useful, the real solution is understanding how Python runs modules. ... Instead of treating it as a standalone file. ... Short answer: No — not directly. Jupyter notebooks do not run code the same way as the terminal.
🌐
HAR Data Extractor
jonathansoma.com › course › foundations-2021 › jupyter-module-not-found
Fixing Module Not Found errors with Jupyter notebook
If you’re getting ModuleNotFoundError in Jupyter, first double-check that you installed the package with pip install. Triple-check you didn’t misspell the package name in your import, otherwise you’ll waste a lot of time reading this page. You probably have multiple versions of Python ...
🌐
Jupyter Notebook
jupyter-notebook.readthedocs.io › en › stable › troubleshooting.html
What to do when things go wrong — Jupyter Notebook 7.6.3 documentation
The Jupyter stack is very complex and rightfully so, there’s a lot going on. On occasion you might find the system working perfectly well, then, suddenly, you can’t get past a certain cell due to import failures. In these situations, it’s best to ask yourself if any new python files were added to your notebook ...
🌐
Physics Forums
physicsforums.com › more sciences and computing › programming and computer science
"import" in Jupyter notebook not working with notebook module.... • Physics Forums
October 1, 2021 - The lesson learned was that the module, as .ipynb, does not work with the import instruction. Are there other more elegant or efficient ways to solve this issue instead of having to convert the notebook module to .py? Thank you! ... Perfect! Thank you pbuk You can also copy/paste the python code into a regular .py file and use import from there, or even put it up on PyPl
🌐
GitHub
github.com › microsoft › vscode › issues › 191871
VSCode Jupyter Notebook is not importing functions from .py file · Issue #191871 · microsoft/vscode
August 31, 2023 - Type: Bug I am currently working on vscode jupyter notebook and on the same directory I have a .py file as presprocessing_function.py and when I try to import certain function it does not recognize it. VS Code version: Code 1.81.1 (6c3e3...
Author: microsoft