You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求解flake8报WPS412错误:__init__.py模块存在逻辑问题

WPS412错误触发原因
  • WPS412是flake8扩展规则集wemake-python-styleguide的专属校验规则,设计初衷是要求__init__.py保持极简定位,仅用来声明包的元信息,不承载任何可执行逻辑。
  • 你当前文件中的大量跨子模块import语句,被规则判定为不属于__init__.py允许的合法内容范畴,因此触发报错。
解决方法

根据你的使用场景可以选择以下任意方案:

方案1:轻量化__init__.py(推荐)

删除所有导入语句,仅保留文档字符串、包元信息(如版本、作者)以及__all__接口声明即可:

"""summary line: finding_hidden_messages_in_dna is a subpackage.

**description**
It is the part of package coursera_bioinformatics_specialization_solutions.
"""

__version__ = "0.1.0"
__all__ = [
    "find_all_approximate_occurrences_of_pattern_with_given_hamming_distance_in_text",
    "find_clumps_in_text",
    "find_dnaa_box_based_on_mismatches_and_reverse_complements",
    "generate_frequency_table",
    "find_hamming_distance",
    "kmp",
    "find_all_occurrences_of_pattern_in_text",
    "find_locations_of_minimum_and_maximum_skew_value",
    "longest_prefix_that_is_also_suffix",
    "find_most_frequent_k_mers_and_respective_reverse_complements_with_mismatches",
    "find_all_most_frequent_k_mers",
    "find_most_frequent_k_mers_with_mismatches",
    "find_neighbors_with_expected_hamming_distance",
    "find_reverse_complement",
    "find_skew_value"
]

上层调用时直接从子模块导入对应接口即可,示例:from finding_hidden_messages_in_dna.clumps_in_text import find_clumps_in_text

方案2:保留包层级直接导入能力

如果你的上层代码已经依赖了直接从包导入接口的写法(如from finding_hidden_messages_in_dna import find_clumps_in_text),可以用惰性导入替换直接导入,避免包加载时执行导入逻辑:

"""summary line: finding_hidden_messages_in_dna is a subpackage.

**description**
It is the part of package coursera_bioinformatics_specialization_solutions.
"""

__all__ = [
    "find_all_approximate_occurrences_of_pattern_with_given_hamming_distance_in_text",
    "find_clumps_in_text",
    # 其余要暴露的接口名按上述格式补充完整
]

def __getattr__(name):
    if name == "find_all_approximate_occurrences_of_pattern_with_given_hamming_distance_in_text":
        from finding_hidden_messages_in_dna.approximate_occurrences_of_pattern_with_given_hamming_distance import find_all_approximate_occurrences_of_pattern_with_given_hamming_distance_in_text
        return find_all_approximate_occurrences_of_pattern_with_given_hamming_distance_in_text
    elif name == "find_clumps_in_text":
        from finding_hidden_messages_in_dna.clumps_in_text import find_clumps_in_text
        return find_clumps_in_text
    # 其余接口的判断分支按上述格式补充完整
    raise AttributeError(f"module {__name__} has no attribute {name}")

方案3:直接忽略该规则

如果不需要遵循该风格规范,可直接在__init__.py文件第一行添加注释跳过检查:

# noqa: WPS412
"""summary line: finding_hidden_messages_in_dna is a subpackage.

**description**
It is the part of package coursera_bioinformatics_specialization_solutions.
"""

# 原有导入语句保留不变
from finding_hidden_messages_in_dna.approximate_occurrences_of_pattern_with_given_hamming_distance import \
    find_all_approximate_occurrences_of_pattern_with_given_hamming_distance_in_text
# 其余import语句保持原样

内容的提问来源于stack exchange,提问作者Deepak Tatyaji Ahire

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 11:09:04