Acessing myclass defined in python file in different folder from jupyter notebook

Viewed 16

I have following folder structure

|
   |---notebook
   |      |
   |      --- MyNoteBook.ipnyb
   |
   |---python
   |     |
   |     --- image_rotation_utils.py

I have following content in python.image_rotation_utils.py

class ImageRotationUtils:
    """ Utils for rotating images."""
    
    def __init__(self, image_path=None, image_name=None, output_path=None, output_annotations_file=None ):
        """ Initializes the class.
        Args:
            image_path: the relative path to the image
            image_name: image file name
            output_path: the relative path where rotated output file is stored.
            output_annotations_file: annotations file where rotated anotations are stored.
        """
        self.image_path = image_path
        self.image_name = image_name
        self.output_path = output_path
        self.output_annotations_file = output_annotations_file

    #other functions defined 

Now in MyNoteBook.ipnyb I have following cells

Cell 1:

import sys
    sys.path.insert(0, '..')
    from python.image_rotation_utils import *

Above is executed successfully now in cell 2 I have below

myUtils = ImageRotationUtils()

I have getting below error

----> 1 ImageRotationUtils()

TypeError: __init__() missing 4 required positional arguments: 'image_path', 'image_name', 'output_path', and 'output_annotations_file'

whye I am getting this error when I have default values in constructor has None in class definition.

I am new to python and trying to put code in productive manner. What mistake I am making. Kindly provide inputs

1 Answers

What you pasted looks ok to me, and your code works for me without errors.

  1. Perhaps something before or after that code is what is spoiling the class definition?
  2. Perhaps you are running this in Jupyter or in IPython and you imported ImageRotationUtils previously, but then changed the code, and tried to reimport again -- and something was wrong with the new reimport so your definition did not get over-written? If that's the case, restart the environment (or the kernel in Jupyter) and rerun the code.
  3. I would suggest putting a simple initialization code, like that constructor line, into the same source file and executing it as a separate process to test if that is the code or the environment issue.

As a matter of convenience and to avoid tweaking sys.path in your code, I would suggest adding your python directory to the PYTHONPATH environment variable before you load your environment, so you can just import.

Related