iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 15
0
Software Development

開源的GIS實作系列 第 15

[day-15] import 自己的函式庫

前言

使用Python,會import很多不同的函式庫,除了import下載的函示庫之外,也能import自己的函式庫。
開發時,不僅有很多版本且code會越來越多。
這時候就需要將code分門別類,讓code可讀。
才不會太久遠的code一時之間無法了解。

實做

我們有這樣的檔案結構,package的資料夾專門放使用的函式。

main.py
package/
├──__init__.py
└──test.py

test.py

def print_hello_world():
    print("hello_world")

main.py

# coding = utf-8

from package.test import print_hello_world
def main():
    print_hello_world()
    
if __name__ == "__main__":
    main()

執行成果

https://ithelp.ithome.com.tw/upload/images/20190920/20112571SBMBmUfupP.jpg

參考資料

Python 的 Import 陷阱


上一篇
[day - 14] git+github版本控制
下一篇
[day-16] 影像處理 - 空間濾波器
系列文
開源的GIS實作30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言