'No module' error for my own function despite having same directory

Viewed 63

I am writing a script file in the Jupyter notebook & I need to call a function. I created another file called myfunctions.py and defined my function called give_ans() there. I will have many more functions here later. But I get an error in the main script file when I use the following command

from myfunctions import give_ans

The error is No module named 'myfunctions'. I use pwd to see their path and it is exactly the same which is 'C:\Users\my name\OneDrive\my_code'. So I don't know what is the cause of this error. Please advise. I have always been a Matlab coder and this is my first attempt here.

Update: I found the problem & fixed it. I gave the solution in my answer below.

1 Answers

I found out what was going wrong. Both of my Jupyter files are of the .ipynb extension whereas the myfunctions.ipynb script that I intend to import should be in .py form. To do so, I went to the command window (Anaconda one in my case), went to the same directory as mentioned in the question, and did the following.

pip install ipynb-py-convert
jupyter nbconvert --to python myfunctions.ipynb

This produced a .py file and then there was no error upon using the import statements as I wrote in the question.

Edit: Anyone reading this answer please also make sure to read the comments below.

Related