close

How do I import module in jupyter notebook directory into notebooks in lower directories?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How do I import module in jupyter notebook directory into notebooks in lower directories in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

How do I import module in jupyter notebook directory into notebooks in lower directories?

  1. How do I import module in jupyter notebook directory into notebooks in lower directories?

    You need to make sure that the parent directory of new_module is on your python path. For a notebook that is one level below new_module, this code will do the trick:

  2. import module in jupyter notebook directory into notebooks in lower directories

    You need to make sure that the parent directory of new_module is on your python path. For a notebook that is one level below new_module, this code will do the trick:

Method 1

You need to make sure that the parent directory of new_module is on your python path. For a notebook that is one level below new_module, this code will do the trick:

import os
import sys
nb_dir = os.path.split(os.getcwd())[0]
if nb_dir not in sys.path:
    sys.path.append(nb_dir)

If you’re further down in the directory hierarchy, you will need to adjust the way nb_dir is set, but that’s all. You should not run this code for a notebook in Jupyter notebooks, since it would add the parent of that directory to the python path, which is probably undesirable.

The reason the import works for notebook1 is that sys.path contains '' (the empty string), which is aways the current directory of the running interpreter (kernel, in this case). A google search for explain python path turns up several good explanations of how python uses PYTHONPATH (aka sys.path).

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read