-1

I'm a little confused with why I'm getting an AttributeError from my enum class I'm importing.

Here's the code I've written:

My enum class that's being imported into another file

from enum import Enum

class Builder(Enum):
    FENDER = 1 
    MARTIN = 2 
    GIBSON = 3 
    COLLINGS = 4 
    OLSON = 5 
    RYAN = 6 
    PRS = 7 
    ANY = 8 

    def __str__(self):
        string = str(self.name)
        return string.lower()

print (Builder.FENDER)
print (Builder.FENDER.__str__())

This is my file that imports my above Builder.py file

import Inventory
import Guitar
import Type
import Builder
import Wood

#class FindGuitarTester:
def initializeInventory(inventory):
    inventory.addGuitar("", 0, Builder.FENDER, "Strat", Type.ELECTRIC, Wood.ALDER, Wood.ALDER)
    inventory.addGuitar("abc123", 19.99, Builder.GIBSON, "Strat", Type.ELECTRIC, Wood.ALDER, Wood.ALDER)
    inventory.addGuitar("def456", 39.99, Builder.PRS, "MMN", Type.ACOUSTIC, Wood.MAPLE, Wood.ROSEWOOD)
#                (self, sn, price, builder, model, mytype, backWood, topWood):

def main():
    import pdb 
    pdb.set_trace()
    inventory = Inventory.Inventory()
    initializeInventory(inventory)


if __name__ == "__main__":
    main()

It complains with this message that "Builder has no attribute 'FENDER'.

$ python3 FindGuitarTester.py 
ACOUSTIC
acoustic
fender
fender
maple
maple
> /Users/username/ooad/ch1/FindGuitarTester.py(17)main()
-> inventory = Inventory.Inventory()
(Pdb) c
Traceback (most recent call last):
  File "/Users/username/ooad/ch1/FindGuitarTester.py", line 35, in <module>
    main()
  File "/Users/username/ooad/ch1/FindGuitarTester.py", line 17, in main
    inventory = Inventory.Inventory()
  File "/Users/username/ooad/ch1/FindGuitarTester.py", line 9, in initializeInventory
    inventory.addGuitar("", 0, Builder.FENDER, "Strat", Type.ELECTRIC, Wood.ALDER, Wood.ALDER)
AttributeError: module 'Builder' has no attribute 'FENDER'

When I tested the Builder.py file by itself, it gave me no such error. I also checked to see if there was some other file named Builder.py or enum.py. Luckily, there are no random files with that name. I also ran python3 to see if the library location was incorrect but no evidence of that too.

$ python3
Python 3.9.6 (default, May  7 2023, 23:32:45) 
[Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import enum
>>> print (enum.__file__)
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/enum.py

Here is the output when I testbed the Builder.py file by itself:

$ python3 Builder.py 
fender
fender
$ 

Can someone enlighten me please? Why am I getting this error when testing the Builder.py file didn't replicate this error message?

Classified
  • 5,759
  • 18
  • 68
  • 99
  • 1
    It sounds like you're doing the Java one-class-per-file thing and expecting `import Whatever` to import the `Whatever` class from `Whatever.py`, when it actually imports the module, not the class. – user2357112 Aug 28 '23 at 10:45
  • 3
    Because Builder is the _module_, as stated in the error message; look at `dir(Builder)`. – jonrsharpe Aug 28 '23 at 10:45

0 Answers0