[解决方案]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 ...
随机推荐
- windows server 远程桌面连接问题。
远程桌面连接相当于 linux 服务器root权限连接 mstsc /admin /v:目标IP mstsc /admin /
- python学习:列表
列表 a = ['abc','bcd','cde','def','efg']print(a)列表的操作:增删改查 1)查:切片print(a[1:3]) #从'bcd'取到'cde',列表取值顾头不顾 ...
- Mac软件安装提示程序已损坏解决方案
---恢复内容开始--- 最近下载好的Mac软件安装时,系统跳出该程序已损坏: 嗯……估计是因为下载了破解版,被系统屏蔽,重设一下安全设置就好: 很兴奋的打开系统偏好设置->安全性与隐私: 然而 ...
- 文件操作,列表实例NiceHexSpiral
fr = open('letter.txt',mode='r',encoding='utf-8') plaincode = fr.read() print('明文:' + plaincode) pri ...
- hive的join
第一:在map端产生join mapJoin的主要意思就是,当链接的两个表是一个比较小的表和一个特别大的表的时候,我们把比较小的table直接放到内存中去,然后再对比较大的表格进行m ...
- Hadoop集群搭建-full完全分布式(三)
环境:Hadoop-2.8.5 .centos7.jdk1.8 一.步骤 1).4台centos虚拟机 2). 将hadoop配置修改为完全分布式 3). 启动完全分布式集群 4). 在完全分布式集群 ...
- CentOS7 防火墙(firewall)的操作命令(转)
安装:yum install firewalld 1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firew ...
- LIS的优化算法O(n log n)
LIS的nlogn的优化:LIS的优化说白了其实是贪心算法,比如说让你求一个最长上升子序列把,一起走一遍. 比如说(4, 2, 3, 1, 2,3,5)这个序列,求他的最长上升子序列,那么来看,如果求 ...
- idea注册码
2019 idea 注册码: N757JE0KCT-eyJsaWNlbnNlSWQiOiJONzU3SkUwS0NUIiwibGljZW5zZWVOYW1lIjoid3UgYW5qdW4iLC ...
- 微信小程序发送短信验证码完整实例
微信小程序注册完整实例,发送短信验证码,带60秒倒计时功能,无需服务器端.效果图: 代码: index.wxml <!--index.wxml--> <view class=&quo ...