[解决方案]SystemError: Parent module '' not loaded, cannot perform relative import的解决方案
缺陷:__mian__不能使用相对导入
PEP 328 Relative Imports and __name__ 中说明:
Relative imports use a module's __name__ attribute to determine that module's position in the package hierarchy. If the module's name does not contain any package information (e.g. it is set to '__main__') then relative imports are resolved as if the module were a top level module, regardless of where the module is actually located on the file system.
即:相对导入在 package 分层中,使用模块的__name__ 属性得到模块的位置。但是如果模块的名字没有包含任何 package 信息(例如,__main__模块),不管是否该模块实际位于文件系统,相对导入会被当作顶层模块导入(a top level module)。
修复
根据 6.4.2. Intra-package References 的建议:
Note that relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application must always use absolute imports.
__mian__ 使用 绝对导入 方式,而其他模块仍使用 相对导入
if __name__ == '__main__':
sys.path.append(os.path.dirname(sys.path[0]))
后话
导入的方式
参考 stackoverflow vaultah 的回答,比较详细,这里不再赘述。
为什么选择相对导入的优势
PEP 328 Rationale for Relative Imports 中说明:
Several use cases were presented, the most important of which is being able to rearrange the structure of large packages without having to edit sub-packages.In addition, a module inside a package can't easily import itself without relative imports.
即:相对导入可以对大型的 python packages 重构时,不用重新编辑 sub-packages;另外,相对导入可以比较轻松地导入自身。
[解决方案]SystemError: Parent module '' not loaded, cannot perform relative import的解决方案的更多相关文章
- python中,下级模块引用上级模块:SystemError: Parent module '' not loaded, cannot perform relative import
当在下级中引用上级时,使用相对导包会出错,SystemError: Parent module '' not loaded, cannot perform relative import 运行test ...
- 【pycharm 警告】unittest RuntimeWarning: Parent module ” not found while handling absolute import
Pycharm 2016.2执行单元测试遇到如下问题: RuntimeWarning: Parent module ‘YOUR_MODULE_HERE’ not found while handlin ...
- 解决- RuntimeWarning: Parent module '...' not found while handling absolute import
Pycharm 升级到 2016.3 以后运行 unittest 报警告如下: 网上查资料说是pycharm的一个已知但未修复的bug,解决办法如下: 使用旧的utrunner.py替换新的utrun ...
- 执行代码出现ImportError:attempted relative import with no known parent package
前言 在这篇文章中,我将会解析 ImportError: attempted relative import with no known parent package 这个异常的原因.当你在运行的py ...
- cocos2dx在Eclipse下编译报错:Cannot find module with tag 'CocosDenshion/android' in import path
在Eclipse下编译cocos2dx项目,报错如下: Android NDK: jni/Android.mk: Cannot find module with tag 'CocosDenshion/ ...
- Python工程:ImportError: attempted relative import with no known parent package
Python工程:ImportError: attempted relative import with no known parent package 解决方法: 1.对每个目录创建的时候都选择创建 ...
- 【问题解决方案】AttributeError: module 'pygal' has no attribute 'Worldmap'
<Python编程:从入门到实践>- 16章-16.2.5制作世界地图 import pygal 后报如标题的error 参考CSDN 解决:AttributeError: module ...
- Cannot find module 'webpack/lib/node/NodeTemplatePlugin' 问题原因和解决方案
当我配置了html-webpack-plugin 打包时报了这个错,查看了一下package.json发现没有webpack,说明使用了全局安装的webapck,导致的版本差异. 这里在本地安装web ...
- 【转】Cannot change version of project facet Dynamic Web Module to 3.1 (Eclipse Maven唯一解决方案)
If you want to use version 3.1 you need to use the following schema: http://xmlns.jcp.org/xml/ns/jav ...
随机推荐
- 用idea 创建一个spring小demo,基于xml文件配置
1.首先,File->new->project ,进入新增项目页面 或者在 2.勾选spring,然后点击下一步 3.修改项目名称和项目位置 进入页面后 5.创建一个spring配置文件 ...
- textarea--去掉空格的办法
我在初次用到textarea多行文本框时,遇到的问题是:默认出现了两行空格,如图: 代码是(错误的写法): 最后才发现,原来是: textarea标签是需要写在同一行,不能换行,正确的写法:
- 记使用aliyun-log-logback-appender 报错no applicable action for [encoder], current ElementPath is [[configuration][appender][encoder]]
依赖: <dependency> <groupId>com.aliyun.openservices</groupId> <artifactId>aliy ...
- web 基础设置
1.设置代码格式为UTF-8 2.运行jsp文档 3.设置自己喜欢的浏览器运行,设置为默认的 找到自己的浏览器位置 点ok Name是名字的意思 为这个浏览器娶一个名字 是什么浏览器就写什么名字 4. ...
- Android第三次作业
制作音乐播放器 实现的功能: 歌曲的播放.暂停.停止.上一首.下一首.歌单列表的显示(获取本地歌曲). 成品图: 功能代码实现: 实现歌曲的播放.暂停.停止.上一首.下一首: public void ...
- c++标准---for循环新用法
贪吃蛇游戏的c++版本,从GitHub上clone下来的,在startinterface.cpp文件中有如下代码: void StartInterface::PrintText() { for ( ...
- Google 浏览器好用插件推荐
Adblock Plus, 免费广告拦截器 Adobe Acrobat 将当前网页转换为 Adobe PDF 文件 Axure RP Extension for Chrome 不仅能绘制出详细的产 ...
- Android图标
在线生成安卓App图标.IOS App图标 https://icon.wuruihong.com
- 浅谈MVC模式与MVVM模式的区别
MVC模式: M:Model(数据模型),用于存放数据 V:View(视图),也就是用户界面 C:Controller是Model和View的协调者,Controller把Model中的数据拿过来给V ...
- Java基础实训