相对进口量为十亿次 - Relative imports for the billionth time

问题:

I've been here: 我来过这里: html

and plenty of URLs that I did not copy, some on SO, some on other sites, back when I thought I'd have the solution quickly. 以及不少我没有复制的URL,有些在SO上,有些在其余站点上,当我觉得我很快就会找到解决方案时。 python

The forever-recurring question is this: With Windows 7, 32-bit Python 2.7.3, how do I solve this "Attempted relative import in non-package" message? 永远存在的问题是:在Windows 七、32位Python 2.7.3中,如何解决此“尝试以非软件包方式进行相对导入”消息? I built an exact replica of the package on pep-0328: 我在pep-0328上构建了该软件包的精确副本: app

package/
    __init__.py
    subpackage1/
        __init__.py
        moduleX.py
        moduleY.py
    subpackage2/
        __init__.py
        moduleZ.py
    moduleA.py

The imports were done from the console. 导入是从控制台完成的。 less

I did make functions named spam and eggs in their appropriate modules. 我确实在相应的模块中建立了名为垃圾邮件和鸡蛋的函数。 Naturally, it didn't work. 天然,它不起做用。 The answer is apparently in the 4th URL I listed, but it's all alumni to me. 答案显然是在我列出的第4个网址中,但对我来讲都是校友。 There was this response on one of the URLs I visited: 我访问的其中一个URL上有此响应: 函数

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 (eg 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. 若是模块的名称不包含任何包信息(例如,将其设置为“ main”),则相对导入的解析就好像该模块是顶级模块同样,不管该模块实际位于文件系统上的哪一个位置。 ui

The above response looks promising, but it's all hieroglyphs to me. 上面的回答看起来颇有但愿,但对我来讲,全都是象形文字。 So my question, how do I make Python not return to me "Attempted relative import in non-package"? 因此个人问题是,如何使Python不返回“未包装的相对导入尝试”? has an answer that involves -m, supposedly. 可能有一个涉及-m的答案。 this

Can somebody please tell me why Python gives that error message, what it means by "non-package", why and how do you define a 'package', and the precise answer put in terms easy enough for a kindergartener to understand . 有人能够告诉我Python为何给出错误信息,“非包装”是什么意思,为何以及如何定义“包装”,以及确切的答案,使幼儿园儿童容易理解 spa


解决方案:

参考一: https://stackoom.com/question/xIaD/相对进口量为十亿次
参考二: https://oldbug.net/q/xIaD/Relative-imports-for-the-billionth-time
相关文章
相关标签/搜索