ValueError: Attempted relative import in non-package


執行:
python deom/scripts/populate.py

ValueError: Attempted relative import in non-package

solve:python import時采用了相對路徑,使用-m運行

執行:

python -m demo.scripts.populate

 
        

 

 

參考:http://my.oschina.net/leopardsaga/blog/97175

1. 從查到的資料來看,關於import路徑的來說,分成3類:
  • absolute import 
import xml
import youpackage.xml
from youpackage import xml
這幾種都算絕對路徑
 
  • relative import
import xml
從這個語句上是看不出來import的是標准庫的xml,還是你的包里的一個庫叫xml。
 
  • explicit relative import
from . import xml
from .xml import some_thing
from ..xml import some_thing
這些以點開頭的import明確的表示了import的是相對路徑,從而避免了普通relative import的麻煩。
 
2. 第二種(relative import)肯定是不推薦的。
  • 在python2.5以前,如果當前包中有一個叫xml的庫,局部的這個xml就會shadow標准庫的xml(和局部變量感覺類似,我犯過好幾次這個錯誤,調試的時候還一定能反應過來)。
  • 在2.5和2.6,這個行為還是一樣的,但是如果有以下這個語句,局部的包將不能覆蓋全局的包
from __future__ import absolute_import
  • 在2.7中,absolute_import的行為變成了默認行為,如果需要應用局部的包,那就得用明確的相對引用了。
文檔是這么描述的,但通過-m運行和直接運行py的行為還是會不一樣,看下面這個例子:
$ python --version
Python 2.6.5
 
$ cat bar/baz.py 
from __future__ import absolute_import
import xml
print xml.__file__
 
$ python bar/baz.py 
/home/huanghao/workspace/test/bar/xml.pyc
 
$ python -m bar.baz
/usr/lib/python2.6/xml/__init__.pyc
 
看上去很奇怪,通過py運行的時候absolute_import好像根本沒有作用,原因在這個blog里有解釋
 
3.所以說relative import麻煩事太多,一定不能用了。但是absolute和explicit relative這兩個到底用哪個?
PEP8中提到:
Relative imports for intra-package imports are highly discouraged. Always use the absolute package path for all imports. Even now that   PEP 328   is fully implemented in Python 2.5, its style of explicit relative imports is actively discouraged; absolute imports are more portable and usually more readable.
 
PEP8還是建議用absolute,即使PEP328實現了explicit relative。
本來我覺得到此就為止了。  但是今天還是有同事非要用explicit relative import,跟我說因為absolute寫起來太長,而且當頂層包重命名時,所有的文件都需要修改。
我搜了半天也沒找到有文章提到abs比rel一定要好的理由,除了PEP8提到的兩個缺乏說服力的詞more portable and more readable。
4.explicit relative import有一個小問題:
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.
 
 
Note that both explicit and implicit 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 should always use absolute imports.
 
PEP 338 -- Executing modules as scripts:   http://www.python.org/dev/peps/pep-0338/
 
雖然PEP338搞定了explicit relative import 在-m執行時的問題,但是搞不定直接文件執行的情況。所以用來做程序入口的模塊必須使用絕對引用。
看一下這個例子:
$ python --version
Python 2.6.5
 
$ cat bar/baz.py 
from . import xml
print xml.__file__
 
$ python -m bar.baz
bar/xml.pyc
 
$ python bar/baz.py 
Traceback (most recent call last):
  File "bar/baz.py", line 1, in <module>
    from . import xml
ValueError: Attempted relative import in non-package

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM