Python module and package, python Module

Source: Internet
Author: User

Python module and package, python Module

1. The. py file can be viewed as a module, which is similar to the class library encapsulated in other languages.

Module category:
Built-in modules
Custom Module
Third-party module (installation required for use)

I want to use the function defined in cal. py.

Cal. py source code:

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

Import_test.py:

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

 

2. built-in properties _ 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$ 

When cal. py is executed, __name _ is interpreted as _ main __. when executed in the module introduction mode, it is not interpreted as a file name. What is the purpose of this feature?

#!/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

This is a small role of _ name _. When a module (. to call the functions or methods defined in another file, we can call the external module through _ name, will not execute such as print add (100,200)

Specific business code. Only use the functions or methods defined in it.

 

Iii. Module loading sequence:

If the current directory has a system module with the same name, the module in the current directory will be imported and the system module will be ignored, for example:

 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

In the current directory, a string module with the same name is defined (the same name as the system's string Module). During execution, the module in the current directory is imported, therefore, capttalize cannot identify the system's string module method.

You only need to delete the string. py string. pyc in the directory to import the system modules normally.

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

 

4. When a directory contains the _ init _. py file, it can be used as a package.

 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)

In test. py, reference the module in the package

 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$ 

There are two other import methods for the import module: alias and direct import method.

 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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.