缺陷:__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的解决方案的更多相关文章

  1. python中,下级模块引用上级模块:SystemError: Parent module '' not loaded, cannot perform relative import

    当在下级中引用上级时,使用相对导包会出错,SystemError: Parent module '' not loaded, cannot perform relative import 运行test ...

  2. 【pycharm 警告】unittest RuntimeWarning: Parent module ” not found while handling absolute import

    Pycharm 2016.2执行单元测试遇到如下问题: RuntimeWarning: Parent module ‘YOUR_MODULE_HERE’ not found while handlin ...

  3. 解决- RuntimeWarning: Parent module '...' not found while handling absolute import

    Pycharm 升级到 2016.3 以后运行 unittest 报警告如下: 网上查资料说是pycharm的一个已知但未修复的bug,解决办法如下: 使用旧的utrunner.py替换新的utrun ...

  4. 执行代码出现ImportError:attempted relative import with no known parent package

    前言 在这篇文章中,我将会解析 ImportError: attempted relative import with no known parent package 这个异常的原因.当你在运行的py ...

  5. 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/ ...

  6. Python工程:ImportError: attempted relative import with no known parent package

    Python工程:ImportError: attempted relative import with no known parent package 解决方法: 1.对每个目录创建的时候都选择创建 ...

  7. 【问题解决方案】AttributeError: module 'pygal' has no attribute 'Worldmap'

    <Python编程:从入门到实践>- 16章-16.2.5制作世界地图 import pygal 后报如标题的error 参考CSDN 解决:AttributeError: module ...

  8. Cannot find module 'webpack/lib/node/NodeTemplatePlugin' 问题原因和解决方案

    当我配置了html-webpack-plugin 打包时报了这个错,查看了一下package.json发现没有webpack,说明使用了全局安装的webapck,导致的版本差异. 这里在本地安装web ...

  9. 【转】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 ...

随机推荐

  1. 日常问题181101: ueditor文本编辑器

    下载地址: https://ueditor.baidu.com/website/download.html#ueditor 把下载好的文件整个复制到根目录(或是,想要存放的目录) 引入css: < ...

  2. Spring源码阅读 源码环境搭建(一)

    ring 源码阅读的搭建(一) 一 下载spring源码 进入官方网页:https://spring.io/projects/spring-framework 进入相关的github位置,下载zip包 ...

  3. SELECT 语句

    常见表的操作 查看数据库的表   show  table 查看表结构     desc 表名 删除表   drop table表 修改表的结构 添加列  alter   table 表名 add 列名 ...

  4. CRT破解版

    1.先去https://www.ttrar.com/html/VanDyke-SecureCRT.html上面下载一个CRT软件 2.下载一个注册机 http://www.ddooo.com/soft ...

  5. Unity进阶----Lua语言知识点(2018/11/08)

    国内开发: 敏捷开发: 集中精力加班堆出来第一个版本 基本没啥大的bug 国外开发: 1).需求分析: 2).讨论 3).分模块 4).框架 5).画UML图(类图class function)(e- ...

  6. C# WPF 使用委托修改UI控件

    近段时间在自学WPF,是一个完全不懂WPF的菜鸟,对于在线程中修改UI控件使用委托做一个记录,给自已以后查询也给需要的参考: 界面只放一个RichTextBox,在窗体启动时开起两个线程,调用两个函数 ...

  7. 逆天的 GRUB

    参考资料 GRUB 的文档在这里:https://www.gnu.org/software/grub/manual/grub/ Linux 的启动过程和 GRUB 的地位 Linux 系统启动的过程是 ...

  8. 《Android插件化开发指南》勘误

    一些常识: 1)全书70个代码例子中,涉及到插件的例子,请先assemble插件的项目,这会在HostApp项目中生成assets目录,并在该目录下plugin1.apk.这样,HostApp才能正常 ...

  9. emWin智能家居主界面设计,含uCOS-III和FreeRTOS两个版本

    第6期:智能家居主界面设计配套例子:V6-910_STemWin提高篇实验_智能家居主界面设计(uCOS-III)V6-911_STemWin提高篇实验_智能家居主界面设计(FreeRTOS) 例程下 ...

  10. Mycat对MySQL进行垂直水平分表分库,读写分离

    1.   MyCAT概述 1.1 背景 随着传统的数据库技术日趋成熟.计算机网络技术的飞速发展和应用范围的扩充,数据库应用已经普遍建立于计算机网络之上.这时集中式数据库系统表现出它的不足: (1)集中 ...