這個問題困擾了我很久了,網上的解決方法都很一致,找來找去都是一樣的解決方法,在匯入包的檔案和執行檔案加入
1 print('__file__={0:<35} | __name__={1:<20} | __package__={2:<20}'.format(__file__,__name__,str(__package__)))
這串代碼,然后在主目錄下新增一個檔案main.py,也寫入這串代碼,
嘗試了很多次,這解決方法對我并不起效果,
最后發現,只要匯入包的時候不用相對參考,就不會出現這種報錯的問題
例子如下:

test_data.py
1 count = 5
test_case.py
1 from . import test_data #相對參考 2 print(test_data.count)
使用這種相對參考的方式,就會報錯

改成絕對參考的方式匯入
1 from test_16 import test_data #絕對參考 2 print(test_data.count)
完美解決匯入報錯的問題,

萬萬沒想到,只是匯入的方式導致的例外問題,答應我,再也不要用相對參考了,必須用絕對參考,絕對參考萬歲!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/153810.html
標籤:Python
上一篇:爬蟲 1
