Python3 模組、包調用&路徑,python3模組包調用
1 ''' 2 以下代碼均為講解,不能實際操作 3 ''' 4 ''' 5 部落格園 Infi_chu 6 ''' 7 ''' 8 模組的優點: 9 1.高可維護性10 2.可以大大減少編寫的代碼量11 12 模組一共有三種:13 1.Python標準庫14 2.第三方模組15 3.應用程式自訂模組16 '''17 # import example # 調用example模組18 # from example import example # 調用example模組中的一個example方法19 '''20 部落格園 Infi_chu21 '''22 23 24 '''25 包的特點:26 1.有__init__.py檔案27 2.有很多模組組成28 '''29 30 # from test import example # test為包名,example模組名,一層嵌套31 # from test.test1 import example # test1、test2均為包名,test1在test中,example為模組名32 # from test.test1.func1 import example # func1是example模組中的一個func1方法33 # import test # test為包名,此命令相當於執行了__init__檔案34 '''35 部落格園 Infi_chu36 '''37 '''38 import 包 或 模組 的區別39 import 包 只是執行了一個__init__.py檔案,並沒有與其他模組產生聯絡,取值時需要加.調用40 import 模組 是直接調用模組41 '''42 43 '''44 路徑解決45 '''46 # import sys,os47 # a = os.path.abspath(__file__) # 得到絕對路徑48 # print(a)49 # print(os.path.dirname(a)) # 得到上一層路徑50 # base_dir = os.path.dirname(os.path.dirname(a)) # 得到上上一層路徑51 # print(base_dir)52 # sys.path.append(base_dir)53 '''54 部落格園 Infi_chu55 '''