mans
mans

Reputation: 18168

__init__ is not found on python class when using in jupyter

I have this code as a class in my InputData.py file

class InputData:
    def __init__(self, file_name):
        self.input_file_name = file_name
        print(self.input_file_name)

and I am trying to use it my jupyter notebook (both notebook and py files are on the same folder)

from  InputData import InputData
input_data=InputData('input file')
print(input_data.input_file_name)

in one cell.

When I ran this code, I am getting this error:

 ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-4288fe35f228> in <module>
      1 from  InputData import InputData
----> 2 input_data=InputData('input file')
      3 print(input_data.input_file_name)

TypeError: InputData() takes no arguments

Why does It say that InputData class has no argument when apparently, it has?

if I change the code to this:

from  InputData import InputData
input_data=InputData()
print(input_data.input_file_name)

I am getting this error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-15-e11cc558c77a> in <module>
      1 from  InputData import InputData
      2 input_data=InputData()
----> 3 print(input_data.input_file_name)

AttributeError: 'InputData' object has no attribute 'input_file_name'

Apparently, the __init__ is not seen and called. What is the problem?

Edit 1

Based on comments I noted that the Jupyter notebook can not see the changes that I made to my class. How can I force Jupyter to see my changes?

Upvotes: 0

Views: 866

Answers (1)

PieCot
PieCot

Reputation: 3639

Try to add a cell at the beginning of your notebook with the following commands and run it:

%load_ext autoreload
%autoreload 2

Now, you can change your code and rerun it in your notebook without restarting the kernel.

Upvotes: 1

Related Questions