關於這句from future import absolute_import的作用:
直觀地看就是說”加入絕對引入這個新特性”。說到絕對引入,當然就會想到相對引入。那么什么是相對引入呢?比如說,你的包結構是這樣的:
pkg/
pkg/init.py
pkg/main.py
pkg/string.py
如果你在main.py中寫import string
那么在Python 2.4或之前, Python會先查找當前目錄下有沒有string.py,
若找到了,則引入該模塊,然后你在main.py中可以直接用string了。
如果你是真的想用同目錄下的string.py那就好,
但是如果你是想用系統自帶的標准string.py呢?
那其實沒有什么好的簡潔的方式可以忽略掉同目錄的string.py而引入系統自帶的標准string.py。
這時候你就需要from future import absolute_import了。
這樣,你就可以用import string來引入系統的標准string.py,
而用from pkg import string來引入當前目錄下的string.py了.