Search code examples
pythonclassmodulepackageattributeerror

python AttributeError not finding a class within a module I am importing


I have a package containing a module called rigfuncs3.py

SDNpackage/
    __init__.py
    rigfuncs3.py

Code:

class rig():

    def __init__(self, rigNumber, leftNozPlate, rightNozPlate, bottomNozPlate, row1_centre_focus):

        self.rigNumber = rigNumber
        self.leftNozPlate = leftNozPlate
        self.rightNozPlate = rightNozPlate
        self.bottomNozPlate = bottomNozPlate
        self.row1_centre_focus = row1_centre_focus

    def getRigName(self):
        return self.rigNumber

A script called play_04.py imports the module rigfuncs from the package. Code:

from SDNpack2 import rigfuncs3

instantiation = rigfuncs3.rig(1,1000.0, 2000.0, 3000.0, 4000.0)
print(dir(instantiation))

rig_num = instantiation.getRigName()

When run play_04 I get the error:

AttributeError: 'rig' object has no attribute 'getRigName'

I have no idea why, any ideas?


Solution

  • I still cannot replicate the problem you're having.

    I've tried to copy your stated folder-structure like this:

    C:.
    │   play_04.py
    │
    └───SDNpack2
            rigfuncs3.py
            __init__.py     <--- empty file
    

    rigfuncs3.py:

    class rig():
        def __init__(self, rigNumber, leftNozPlate, rightNozPlate, bottomNozPlate, row1_centre_focus):
            self.rigNumber = rigNumber
            self.leftNozPlate = leftNozPlate
            self.rightNozPlate = rightNozPlate
            self.bottomNozPlate = bottomNozPlate
            self.row1_centre_focus = row1_centre_focus
    
        def getRigName(self):
            return self.rigNumber
    

    play_04.py:

    from SDNpack2 import rigfuncs3
    
    instantiation = rigfuncs3.rig(1, 1000.0, 2000.0, 3000.0, 4000.0)
    print(instantiation)
    print(instantiation.__dict__)
    
    rig_num = instantiation.getRigName()
    print(rig_num)
    

    I change your call of dir(instantiation) into instantiation.__dict__ to just show the attributes of the object, not all built-in variables.

    Output:

    <SDNpack2.rigfuncs3.rig object at 0x02B1E628>
    {'rigNumber': 1, 'leftNozPlate': 1000.0, 'rightNozPlate': 2000.0, 'bottomNozPlate': 3000.0, 'row1_centre_focus': 4000.0}
    1
    

    The only way I can replicate the problem in of itself is if I either change the indentation of getRigName or if I remove it all together from the class.

    Your code works the way you have coded it, I see no actual problem here.