Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Enum AttributeError: module 'enum' has no attribute 'StrEnum' [closed]

I am working on Windows 11, using Python 3.11; I'm working on the following code snippet, which comes from from the Python docs on enum.StrEnum

import enum
from enum import StrEnum


class Build(StrEnum):
    DEBUG = enum.auto()
    OPTIMIZED = enum.auto()

    @classmethod
    def _missing_(cls, value):
        value = value.lower()
        for member in cls:
            if member.value == value:
                return member
        return None

print(Build.DEBUG.value)

When I run the code, I get the following error: ImportError: cannot import name 'StrEnum' from 'enum'. I made the following changes:

import enum

class Build(enum.StrEnum):  # <--change made here
    DEBUG = enum.auto()
    OPTIMIZED = enum.auto()

Now when I run the code I get an AttributeError: module 'enum' has no attribute 'StrEnum'. I am working with PyCharm, I can see StrEnum class is apart of the enum library. So, can someone please explain what I am doing wrong, or whats going on here. Thanks for any help!

like image 788
Seraph Avatar asked May 04 '26 07:05

Seraph


1 Answers

I had the same problem, and double checked that Python used for the PyDev interpreter in the project is 3.11 (3.11.10 to be specific), because StrEnum is a new feature of Python 3.11. Also tried adding enum to Forced Builtins of the PyDev interpreter and (un)settings Additional syntax validation. It didn't help.

Re-creating the PyDev interpreter (removing and adding the virtual environment Python binary) helped.

like image 112
saaj Avatar answered May 05 '26 20:05

saaj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!