Python中Enum使用方法及自定义Enum文件导入报错求助
Hey there! Let's break this into two clear parts: first, how to work with Enum in Python, then we'll fix that frustrating import error you're hitting.
Enums are great for defining fixed sets of related values—they make your code more readable and less error-prone than using raw strings or integers. Here's a quick walkthrough:
Import the Enum class
First, grab the baseEnum(or specialized types likeIntEnum) from the standard library:from enum import EnumDefine your Enum class
Create a subclass ofEnumand list your members with their values:class UserRole(Enum): ADMIN = "admin" EDITOR = "editor" VIEWER = "viewer"If you need integer-based enums (useful for database IDs or APIs), use
IntEnuminstead:from enum import IntEnum class OrderStatus(IntEnum): PENDING = 1 SHIPPED = 2 DELIVERED = 3Use your Enum
- Access members directly:
UserRole.ADMIN - Get the underlying value:
UserRole.ADMIN.value(returns"admin"here) - Look up a member by value:
UserRole("editor") - Iterate over all members:
for role in UserRole: print(f"{role.name}: {role.value}")
- Access members directly:
Import errors usually boil down to file structure, naming issues, or syntax mistakes. Let's go through the most common fixes step by step:
1. Double-check file naming & case sensitivity
Python (and most Unix-like systems) is case-sensitive when it comes to filenames. If your Enum file is named myenum.py, you need to import it as import myenum—not import Myenum (unless your file is actually named Myenum.py, though Python convention prefers snake_case filenames like my_enum.py).
2. Make sure files are in the same directory
Your main script (the one importing the Enum) and your Enum file need to live in the same folder. If they're in different directories, you have two options:
- Add the Enum's directory to Python's path temporarily:
import sys sys.path.append("/path/to/the/folder/containing/myenum.py") import myenum - Turn the folder into a proper Python package by adding an empty
__init__.pyfile, then import using relative or absolute paths. For example, if your structure looks like this:
You can import with:my_project/ ├── main.py └── enums/ ├── __init__.py └── myenum.pyfrom enums.myenum import UserRole # or import enums.myenum as myenum
3. Avoid naming conflicts
Don't name your Enum file enum.py—this will clash with Python's built-in enum module, causing all sorts of weird errors. Stick to unique names like my_enums.py or app_enums.py.
4. Verify your import syntax
If your Enum class is called UserRole inside myenum.py, make sure you're referencing it correctly:
- If you use
import myenum, you need to access it asmyenum.UserRole - If you want to use just
UserRole, usefrom myenum import UserRole - If you have multiple Enums in the file, you can import all with
from myenum import *(though this is less clean for larger projects)
5. Check for typos
It sounds obvious, but double-check every part of your import:
- Did you misspell the filename? (
myenumvsmyenums) - Did you misspell the Enum class name? (
UserRolevsUserRoles)
Example Working Setup
Let's put it all together with a concrete example:myenum.py:
from enum import Enum class UserRole(Enum): ADMIN = "admin" EDITOR = "editor" VIEWER = "viewer"
main.py (in the same folder):
# Option 1: Import the whole module import myenum print(myenum.UserRole.ADMIN.value) # Output: admin # Option 2: Import the specific Enum from myenum import UserRole print(UserRole.EDITOR) # Output: UserRole.EDITOR
If you're still hitting issues, feel free to share your file structure, exact error message, and code snippets—that'll help narrow it down faster!
内容的提问来源于stack exchange,提问作者성기덕




