[解决方案]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 ...
随机推荐
- 雕刻机制作 PCB 指南
之前使用过感光蓝油制作过 PCB,虽然感光法精度高,但个人制作耗时耗力,发给厂家周期又很长.看到国外的网友使用雕刻机制作 PCB 视频之后.几番周折之后还是成功了.有感于网上几乎没有一份完整的雕刻机 ...
- Numpy 基础运算2
# -*- encoding:utf-8 -*- # Copyright (c) 2015 Shiye Inc. # All rights reserved. # # Author: ldq < ...
- ThreadLocal与线程池使用的问题
感谢博主的这篇分享,见 https://www.cnblogs.com/qifenghao/p/8977378.html 在今天的面试中,突然被考官问了这个问题,当时脱口而出的是 threadloca ...
- CentOS7x64 防火墙配置
Firewall开启常见端口命令: firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --zone=publi ...
- c# asp.net mvc使用斑马GK888t打印机打印标签
前言 c#语言,asp.net mvc,南京都昌电子病历模板工具(类似word),斑马GK888t,打印手腕带和标签纸. 实现步骤为:在页面上显示一个或多个都昌模板工具,点击页面上的button,出现 ...
- [JZOJ3615]【NOI2014模拟】数列(平面几何+二维线段树)
Description 给定一个长度为n的正整数数列a[i]. 定义2个位置的f值为两者位置差与数值差的和,即f(x,y)=|x-y|+|a[x]-a[y]|. 你需要写一个程序支持2种操作(k都是正 ...
- apicloud实现各种自定义弹层组件
- Katalon Studio之swagger中的API导入
约束条件: swagger中一定要在注解@ApiOperation中设置nickname的唯一值,例如: @ApiOperation(value="新增用户",notes=&quo ...
- nmon监控分析
一.下载软件安装 wget http://sourceforge.net/projects/nmon/files/nmon_linux_14i.tar.gz tar xf nmon_linux_14i ...
- sublime text3 在 14.04.1-Ubuntu 下的中文输入
1.安装 fcitx sudo add-apt-repository ppa:fcitx-team/nightly // 添加FCITX仓库. sudo apt-get update // 更新仓库. ...