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

Python模块导入失败求助:无法从目录导入模块

模块导入错误:ModuleNotFoundError: No module named 'style_generator'

问题场景

根目录下的main.py导入modules目录中的out_handler.py,但out_handler.py导入同目录下的style_generator.py时触发报错,报错信息如下:

Traceback (most recent call last):
  File "c:\Users\mayan\Desktop\food data app with soonacular\main.py", line 1, in <module>
    from modules import out_handler
  File "c:\Users\mayan\Desktop\food data app with soonacular\modules\out_handler.py", line 2, in <module>       
    import style_generator
ModuleNotFoundError: No module named 'style_generator'

文件结构

food data app with soonacular/
├── main.py
└── modules/
    ├── __init__.py
    ├── out_handler.py
    ├── style_generator.py
    └── text_generator.py

相关代码

main.py

from modules import out_handler
d = out_handler.out()
d.Print(text='rohan')

modules/out_handler.py

import os
import style_generator
import text_generator

class terminal:
    __data = ''''''
    def get_data() -> str:
        return terminal.__data
    
    def set_data(text: str) -> None:
        terminal.__data = text

    def add_data(text) -> None:
        terminal.__data += text

    def clear() -> None:
        os.system('cls')

class out:
    def Print(self,text,color='',background='',style='',justtify='left',font=''):
        render = ''
        match font:
            case '':
                render = style_generator.Text.out(text,color=color,background=background,style=style)
            case _:
                render = text_generator.styles.style(text,font=font,color=color,background=background,styles=style,justify=justtify)

        terminal.set_data(render)
        print(render)

out().Print(text="Aryan",color='red',style='bright',font='3-d',justtify='right')

modules/style_generator.py

from pyfiglet import Figlet
from colorama import Fore, Back, Style, init
from text_generator import Text

class styles:
    init(autoreset=True)

    def style(text,font,color='',background='',styles = 'normal',justify= 'left'):
        f = Figlet(font,justify=justify)
        result = f.renderText(text)
        return Text.add_style(styles) + Text.add_color(color) + Text.add_background(background) + result

解决方案

问题源于同目录模块的导入方式错误,以下两种方案可解决问题:

方案1:使用相对导入(推荐)

修改out_handler.py中的导入语句,用.表示当前目录:

import os
from . import style_generator
from . import text_generator

同时修改style_generator.py中的导入语句:

from pyfiglet import Figlet
from colorama import Fore, Back, Style, init
from . import text_generator

方案2:使用绝对导入

基于项目根目录的完整路径导入,修改out_handler.py:

import os
from modules import style_generator
from modules import text_generator

修改style_generator.py:

from pyfiglet import Figlet
from colorama import Fore, Back, Style, init
from modules import text_generator

注意事项

  • 确保modules目录下存在空的__init__.py文件(已添加无需改动),这是Python识别包的必要标志。
  • 必须从项目根目录执行main.py,不要进入modules目录单独运行文件,否则会破坏包结构的识别逻辑。

内容的提问来源于stack exchange,提问作者Mayank Kumar Gupta

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 11:35:28