Skip to content Skip to sidebar Skip to footer

Module Not Found During Import In Jupyter Notebook

I have the following package (and working directory): WorkingDirectory-- |--MyPackage-- | |--__init__.py |

Solution 1:

I'm pretty sure this issue is related and the answer there will help you: https://stackoverflow.com/a/15622021/7458681

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

Solution 2:

two lines of code will solve this,

#list the current work dir
os.getcwd()
#change the current work dir
os.chdir()

change the path, and import module, have fun.

Solution 3:

if you face module not found on jupyter environment you had to install it on jupyter environment instead of installing it on command prompt

by this command(for windows) on jupyter

!pip install module name

after that you can easily import and use it. Whenever you want to tell jupyter that this is system command you should put ( ! ) before your command.

Solution 4:

You can do that by installing the import_ipynb package.

pip install import_ipynb

Suppose you want to import B.ipynb in A.ipynb, you can do as follows:

In A.ipynb:

import import_ipynb
import B as b

Then you may use all the functions of B.ipynb in A.

Solution 5:

The reason is that your MyPackage/__init__.py is ran from the current working directory. E.g. from WorkingDirectory in this case. It means, that interpreter cannot find the module named module1 since it is not located in either current or global packages directory.

There are few workarounds for this. For example, you can temporarily override a current working directory like this

cwd = os.getcwd()
csd = __path__[0]
os.chdir(csd)

and then, after all a package initialization actions like import module1 are done, restore "caller's" working directory with os.chdir(cwd).

This is quite a bad approach as for me, since, for example, if an exception is raised on initialization actions, a working directory would not be restored. You'll need to play with try..except statements to fix this.

Another approach would be using relative imports. Refer to the documentation for more details.

Here is an example of MyPackage/__init__.py that will work for your example:

from .module1import *

But it has few disadvantages that are found rather empirically then through the documentation. For example, you cannot write something like import .module1.


Upd: I've found this exception to be raised even if import MyPackage is ran from usual python console. Not from IPython or Jupyter Notebook. So this seems to be not an IPython itself issue.

Post a Comment for "Module Not Found During Import In Jupyter Notebook"