7

I have a Jupyter notebook, I want to use local python functions from other folders in my computer. When I do import to these functions I get this error: "ModuleNotFoundError: No module named 'xxxxxxxxxxxxx'

  • I'm using anaconda as the python interpreter
erez
  • 399
  • 1
  • 6
  • 17

3 Answers3

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!

tda
  • 2,045
  • 1
  • 17
  • 42
  • Added to answer above. – tda May 03 '18 at 13:04
  • Thanks TD-Asker. When I'm using the third option I get this message: "No files/directories in C:\rnd\ErezLaxBranch\python\GeometricUtils (from PKG-INFO)" Any idea what is the problem? – erez May 03 '18 at 13:11
  • Please report back what option was best and I can highlight this in my answer here for future visitors. – tda May 03 '18 at 13:14
  • Hi again,Thanks for your help. I'm trying now all the options and have no success.. My case is that I have 2 folders with .py files inside, some of the files in the folders import functions from other etc. How can I include these two folders with all their files in the path? – erez May 06 '18 at 07:38
  • You just need to add both folders to your system path. #1 or #3 should work if you do it for each folder. – tda May 08 '18 at 07:19
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
  • This is an exact copy of my answer? Can you point out what's different? – tda May 03 '18 at 13:09
  • different in that, you use constant path_to_file, i use dynamic path_to_file. i know that i use sub folder and i do not know were on PC my program be. your variant is also good. and can help if know path_to_file on PC – Konstantin Kozlenko May 03 '18 at 13:24
1

If you aren't already, try using the full pathname of the function in your code.

from Folder1.Folder2.FileName import ModuleName

someVar = ModuleName(params)

Folder1 would be a folder in the same directory as your main program. Also, create an empty file called __init__.py in every folder you import from.

Ctrl S
  • 1,053
  • 12
  • 31