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:
Answer from tda on Stack OverflowI 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!
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!
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 will not import module
python - Module not found during import in Jupyter Notebook - Stack Overflow
Python in terminal finds module, jupyter notebook does not
Installed package won't import in notebook
Hello All,
I am also working (on a different project) with the Jupiter notebook and it cannot locate a module I wrote that is in the same folder as the module I am working in. It has to do with the fact that Jupiter uses web client for the notebooks but the documentation on the Jupiter website to rectify the problem reads like Ancient Greek to me! Any help is appreciated.
I'm pretty sure this issue is related and the answer there will help you: "ImportError: No module named" when trying to run Python script
tl;dr: the cwd of the notebook server is always the base path where you started the server, no matter was running import os os.getcwd() says. Use import sys sys.path.append("/path/to/your/module/folder").
I ran it with some dummy modules in the same structure as you had specified, and before modifying sys.path it wouldn't run and after it would.
Understand these two functions, and your problem will be solved.
# List the current work directory
os.getcwd()
# Change the current work directory
os.chdir()
Change the path, and import module, and have fun.
Sometimes it won't work. Try this:
import sys
# 'sys.path' is a list of absolute path strings
sys.path.append('/path/to/application/app/folder')
import file
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.
Open a new terminal window and see if this helps. If not, proceed with 2.
Start a standard Python session from the terminal and type this:
>>> import sys >>> sys.executableDo the same in the notebook:
In [1]: import sys sys.executableCompare the results. Hopefully, this gives you a clue what is going on.
Try the following:
source activate tensorflowproblem
conda install notebook ipykernel
ipython kernel install --user
It sounds like your tensorflowproblem env doesn't have an ipython kernel, but another (probably your root) env does. When you are launching Jupyter, it is putting you into the env with the kernel but without the tensorflow package. If you haven't run the above lines yet, you could test this theory by opening a notebook and running:
import sys
sys.path
If the first few paths don't show 'envs/tensorflowproblem' as part of them it would explain the issue.
For jupyter notebook, the following python package is useful for managing which python kernel / anaconda environment to run a notebook with:
pip install environment_kernels
Run this install either in your default environment, or a specific one. After starting jupyter notebook you should see the options for your anaconda environments in two places:
- options to start a new notebook
- in the
kernel > change kerneldropdown for an existing notebook.
For more details, I found this post helpful!
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.