3

I am running Python 3 via Anaconda on Windows 10. I am having trouble importing my own modules into Jupyter workbooks. E.g., if I try import fibo for a module named 'fibo.py' I get the following error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-4-4105c89a6fa8> in <module>()
----> 1 import fibo

ImportError: No module named 'fibo'

I have tried three things: 1) using sys.path.append("path\to\fibo_folder") 2) Changing the PATH and PYTHONPATH environment variables to include "path\to\fibo_folder" 3. Navigating the Jupyter Notebook to the same directory as 'fibo' is installed in.

Note that I have also included an empty .py file in the same directory as fibo called 'init.py'

Only 3) seems to work, but this is impractical if I am using modules stored in different folders (they need to be kept separate). I am sure 1) and 2) should work, but they don't seem to.

How may I import my own modules into Jupyter?

The User
  • 55
  • 3
  • 11
  • If you modify `sys.path` (or `PYTHONPATH`), you need to add the directory containing `fibo` (e.g. `path/to/`), not fibo itself. – Thomas K Apr 02 '16 at 16:35
  • This is answered here: https://stackoverflow.com/questions/39299838/how-do-i-import-module-in-jupyter-notebook-directory-into-notebooks-in-lower-dir – nesaboz Jul 07 '17 at 23:22

1 Answers1

1

sys.path.append("/path/to/fibo file") should have solved the issue. (Note that in your question you have used "\" while giving the path, which is an escape character and is wrong and it should be "/". Not sure whether it is a typo in the question, but just mentioning it for the sake of completion.)

But like you mentioned in the question that sys.path.append() did not work, here is one common place where you might have gone wrong.

In the respective code (.py or .ipynb) where you need to import fibo, you should run the sys.path.append("/path/to/fibo file") every time you run the respective .py or .ipynb.

Meaning, it is not like you open the terminal, run python command, and type sys.path.append("/path/to/fibo file") in the terminal, close it and then run your .py or .ipynb file. This will not work and will throw the above-said error in your question.

sys.path.append() is a session variable and should be run every time you run your respective code in that particular .py or .ipynb file.

This should get

1) and 2)

working!

(Also, (I know, trivial) re-check the path to the "fibo" file is correct)

Sushanth
  • 2,224
  • 13
  • 29