Python导包失败原因及解决方法
问题描述
Python在调试的时候提示:ImportError: attempted relative import with no known parent package
分析与解决方案
这种情况一般是使用了相对路径来引入包,例如
From .stdmae_arch import MaskedAutoencoderViT
修改方案:
①使用完整路径来引入包。这里的完整路径指的是从项目的根目录开始的子文件夹目录。
例如目录结构是:
Basicts
→ stdmae
→stdmae_arch
xxx.py (里面包含要导入的类)
那么绝对路径就是from stdmae.stdmae_arch import MaskedAutoencoderViT
②在运行文件的编辑配置中,配置文件的工作目录为整个项目的根目录。然后再执行run或debug操作。
上述两个步骤建议都完成,以避免导包失败。
Python导包失败原因及解决方法
https://runsstudio.github.io/2025/11/03/Python导包失败原因及解决方法/