Nick X Tsui
Nick X Tsui

Reputation: 2912

iPython Notebook (Python 3): ImportError: No module named

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.

Upvotes: 1

Views: 3359

Answers (2)

Ioannis Nasios
Ioannis Nasios

Reputation: 8527

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\\')

Upvotes: 1

lucians
lucians

Reputation: 2269

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..

Upvotes: 1

Related Questions