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