程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Python3 importing enum from directory

发布于2025-01-05 08:59     阅读(666)     评论(0)     点赞(11)     收藏(1)


I need to import enum class which sits in dir A from some other file which is in dir B. So I have the following:

- enums
 -- __init__.py
 -- flavors.py
- setUp
 -- test.py
flavors.py
from enum import Enum, unique


@unique
class Flavor(Enum):
    VANILLA = "vanilia"
    CHOCOLATE = "chocolate"
    MINT = "mint"

In the test.py I just need to import this enum, but whatever way I chose getting

ModuleNotFoundError: No module named 'enums'


解决方案


I think what you're trying to achieve is something like this: For example, you wanna use the Enum (Flavor) in a new Enum you create in test.py.

In your test.py file, You can try something like:

from enums.flavors import Flavor


class TestEnum(Flavor):
    BROWN_CHOCOLATE = "brownchocolate"
    PEPPERMINT = "peppermint"


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/2046773/a907460fe2acb9fa4c65/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

11 0
收藏该文
已收藏

评论内容:(最多支持255个字符)