python模組與包,python模組

來源:互聯網
上載者:User

python模組與包,python模組

一、.py檔案可以看做一個模組,模組類似其他語言中封裝的類庫

模組分類:
  內建模組
  自訂模組
  第三方模組(需要安裝才能使用)

我想要使用cal.py中定義的函數,可以這樣做

cal.py原始碼:

#!/usr/bin/python#coding:utf-8def add( a, b ):    return a + b

import_test.py要使用add函數:

#!/usr/bin/python#coding:utf-8import calprint cal.add( 10, 20 )

 

二,內建屬性__name__

cal.py

#!/usr/bin/python#coding:utf-8def add( a, b ):    return a + bprint __name__
ghostwu@ghostwu:~/python$ python cal.py__main__ghostwu@ghostwu:~/python$ python import_test.py cal30ghostwu@ghostwu:~/python$ 

當執行cal.py本身時,__name__被解釋成__main__, 通過模組引入方式執行時,別解釋成檔案名稱,這個特性有什麼用?

#!/usr/bin/python#coding:utf-8def add( a, b ):    return a + bif __name__ == '__main__':    print add( 100, 200 )
ghostwu@ghostwu:~/python$ python import_test.py 30ghostwu@ghostwu:~/python$ python cal.py300

這就是__name__的一個小作用,當一個模組(.py檔案)被別的檔案調用時,一般是想調用它裡面定義的函數或者方法,我們可以通過__name__讓外部模組調用方式,不會執行到類似print add( 100, 200 )這樣的

具體業務代碼。只用到裡面的函數或者方法定義。

 

三、模組載入順序:

目前的目錄如果有同名的系統模組,那麼目前的目錄的模組會被import,系統模組會被忽略,如:

 1 ghostwu@ghostwu:~/python/module$ ls 2 import_test.py  string.py 3 ghostwu@ghostwu:~/python/module$ cat string.py  4 #!/usr/bin/python 5 #coding:utf-8 6  7 def add( a, b ): 8     return a + b 9 ghostwu@ghostwu:~/python/module$ cat import_test.py 10 #!/usr/bin/python11 #coding:utf-812 import string13 str = 'ghostwu'14 print string.capitalize( str )15 16 ghostwu@ghostwu:~/python/module$ python import_test.py 17 Traceback (most recent call last):18   File "import_test.py", line 8, in <module>19     print string.capitalize( str )20 AttributeError: 'module' object has no attribute 'capitalize'21 ghostwu@ghostwu:~/python/module$ ls -a22 .  ..  import_test.py  string.py  string.pyc

在目前的目錄下,定義了一個同名的string模組( 指的是與系統的string模組同名 ),由於執行的時候,目前的目錄的模組被import了,所以識別不了系統string模組的方法capttalize.

只要刪除目錄下的string.py string.pyc,就能正常import系統的模組 

1 ghostwu@ghostwu:~/python/module$ ls -a2 .  ..  import_test.py  string.py  string.pyc3 ghostwu@ghostwu:~/python/module$ rm string.py string.pyc4 ghostwu@ghostwu:~/python/module$ ls -a5 .  ..  import_test.py6 ghostwu@ghostwu:~/python/module$ python import_test.py 7 Ghostwu

 

四,當一個目錄下有__init__.py檔案,就可以當做一個包來用

 1 ghostwu@ghostwu:~/python/package_test$ pwd 2 /home/ghostwu/python/package_test 3 ghostwu@ghostwu:~/python/package_test$ ls -a 4 .  ..  calc.py  calc.pyc  fab.py  fab.pyc  __init__.py  __init__.pyc 5 ghostwu@ghostwu:~/python/package_test$ cat calc.py 6 #!/usr/bin/python 7 #coding:utf-8 8  9 def add( a, b ):10     return a + b11 12 def sbb( a, b ):13     return a - b14 15 def mul( a, b ):16     return a * b17 18 def div( a, b ):19     return a / b20 21 oper = { '+' : add, '-' : sbb, '*' : mul, '/' : div }22 23 def mySwitch( x, o, y ):24     return oper.get( o )( x, y )25 26 ghostwu@ghostwu:~/python/package_test$ cat fab.py27 #!/usr/bin/python28 #coding:utf-829 30 def fab( n ):31     if n == 0:32         return 133     else:34         return n * fab( n - 1)

在test.py中,引用包中的模組

 1 ghostwu@ghostwu:~/python/package_test$ cd .. 2 ghostwu@ghostwu:~/python$ ls -a 3 .  ..  module  package_test  test.py  tmp 4 ghostwu@ghostwu:~/python$ cat test.py  5 #!/usr/bin/python 6 #coding:utf-8 7  8 import package_test.calc 9 import package_test.fab10 11 print package_test.calc.add( 10, 20 )12 print package_test.fab.fab( 6 )13 ghostwu@ghostwu:~/python$ python test.py 14 3015 72016 ghostwu@ghostwu:~/python$ 

匯入模組的另外兩種方式: 別名與直接匯入方法

 1 ghostwu@ghostwu:~/python/package_test$ ls 2 calc.py  calc.pyc  fab.py  fab.pyc  __init__.py  __init__.pyc 3 ghostwu@ghostwu:~/python/package_test$ python 4 Python 2.7.12 (default, Dec  4 2017, 14:50:18)  5 [GCC 5.4.0 20160609] on linux2 6 Type "help", "copyright", "credits" or "license" for more information. 7 >>> import calc 8 >>> calc.add( 10, 20 ) 9 3010 >>> import calc as c11 >>> c.add( 100, 200 )12 30013 >>> from calc import add14 >>> add( 1, 2 )15 3

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.