1

First of all, I am really a python idiot, and this is my first python test.

I am running a test_predictors.ipynb file using Jupyter. I ran into an "ImportError: No module named" error when executing the test_predictors.ipynb file by block, like following:

enter image description here

The decisioni_tree.py is another .py file in the same folder as test_predictors.ipynb. calculate_information_gain, decision_tree_train, decision_tree_predict are all functions defined in decisioni_tree.py. The following picture is showing the file layout:

enter image description here

I searched a lot of threads, tried putting all .py files into a subfolder, or adding a leading dot in front of decisioni_tree, or adding full path to decisioni_tree, but none of these worked.

I also read PEP, but it does not make much sense to me. Now I am really clueless. I guess it was the path problem, but I don't figure out the logic behind how python arrange their path. I am wondering anyone can give some pointers? How should I solve this problem? Thanks.

I am using Windows 10, with I think Python 3.4/3.5 installed.

Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73
  • I barely use Jupyter notebooks, so this is a stab in the dark. What happens if you create an empty file in the same directory called `__init__.py` (double underscore on each side) and then try? – roganjosh Sep 27 '17 at 06:02
  • @roganjosh, that was my first thought. I don't have any issues importing from a local .py file with Jupyter on Ubuntu. Worth a shot though - could be that Windows behaves differently. – Andrew Guy Sep 27 '17 at 06:06
  • 1
    @roganjosh I added an empty file named $__init__.py$ like you suggested, but unfortunately the same error occurred. – Nick X Tsui Sep 27 '17 at 06:07
  • 1
    Have you tried to restart kernel? – Sraw Sep 27 '17 at 06:09
  • @Sraw Sorry, I am really python newbie. How to restart kernel? I can try to restart Jupyter, but I don't know if that restarts the kernel. – Nick X Tsui Sep 27 '17 at 06:11
  • @Sraw So I restarted Jupyter, deleted 'test_predictors.ipynb' from the Jupyter webpage, then re-added, ran it, but the same error occurred. I don't know if I restarted kernel? – Nick X Tsui Sep 27 '17 at 06:14
  • Yes you definitely restart kernel if you restart whole jupyter. Could you try to create a `test.py` which only contains `import decisioni_tree`? And could you try to copy all these files to a normal folder? – Sraw Sep 27 '17 at 06:16
  • @Sraw OK I created a folder called "normal" in the same folder as $$test_predictors.ipynb$$, then I cut all the .py files into the normal folder. then I created test.py file which only contains import decision_tree, and this test.py file is in the same folder as normal folder and test_predictors.ipynb. I don't know if I am doing what you mean? I guess I also don't know what I am doing. – Nick X Tsui Sep 27 '17 at 06:25
  • I mean you can try to run `test.py` to test if it is a problem about jupyter. If running in normal python script works well, it may be a problem related to jupyter. And I suppose if it is caused by your folder is a sync folder of dropbox. What I mean `normal folder` is exactly a common, normal folder. – Sraw Sep 27 '17 at 06:46
  • @Sraw I copy the whole folder to a non-sync place instead of dropbox, still the problem occurred. Then I use command line to execute the test.py which only has the import decision_tree line, nothing happened, no error message, just nothing. What does that mean? – Nick X Tsui Sep 27 '17 at 06:56
  • It exit too fast to make sense. Add a line `input()` to block the script. – Sraw Sep 27 '17 at 07:01
  • @Sraw Yeah, now it stops there for the input like the C++ getchar(). So that means test.py works with no problem? – Nick X Tsui Sep 27 '17 at 07:42
  • Yeah, so there is no problem about your scripts structure. All problems are caused by jupyter. Try reinstall jupyter or don't load `auto reload` (delete first cell). – Sraw Sep 27 '17 at 08:45

2 Answers2

1

Seems to me that you cannot import any script from your working directory. This shouldn't be happening. I suggest you take a look at your python paths by running in your notebook:

import sys
print sys.path

if your home directory or directory of notebooks, something like 'C:\\Users\\yourusername\\.ipython' doesn't show up then you probably need add it as a path. try adding your home directory to path:

sys.path.append('C:\\Users\\yourusername\\')

or

sys.path.insert(1, 'C:\\Users\\yourusername\\')
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Ioannis Nasios
  • 8,292
  • 4
  • 33
  • 55
1

Try this:

sys.path.insert(0, 'directory_of_the_pyfile_you_want_to_import')

import FOLDER.file

where FOLDER is the module name and file is the .py file you want to import.

This if the __init__.py solution don't work..

lucians
  • 2,239
  • 5
  • 36
  • 64