Python Modules and Packages

Source: Internet
Author: User

Modules and packages are introduced in the following format, followed by a detailed illustration

Import file name

From directory name import file name

From directory name. File name Import class name


A. Module

1. The module can make the code reusable, reduce the workload, easy to maintain; Implement code sharing, multi-person development, LIB library reference


The module is the basic way that Python organizes the code, and the Python program (. py) can be run alone, or it can be called by other programs, and when it is executed, it can be seen as a module, which simply says: A. py file is a module , Module name is the same as file name

Existing two files zhzhgo.py and module.py, file zhzhgo.py content is: print "I ' m zhzhgo.", File module.py file contents: Import Zhzhgo, For module.py, Zhzhgo is a module that performs module.py and prints the result:

>>>

I ' m zhzhgo.

>>>


2.reload () for heavy-duty modules

The module.py content is now modified as follows:

Import Zhzhgoimport Zhzhgo

You can assume that the contents of the Zhzhgo module will be printed two times, actually running as follows, in order to prevent namespace collisions, only one

>>>

I ' m zhzhgo.

>>>

If you want to execute two times, you can use reload () to force the module to be loaded, and a naming conflict will be overwritten

Import Zhzhgoreload (Zhzhgo)

The results are printed as follows:

>>>

I ' m zhzhgo.

I ' m zhzhgo.

>>>


Examples of common application reload ():

site.py Modify SYS, note that the following code is in C:\Python27\Lib\site.py:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/14/wKioL1Ww8XPzFGKKAAJ7RdpIQhk071.jpg "style=" float: none; "title=" Site1.png "alt=" Wkiol1ww8xpzfgkkaaj7rdpiqhk071.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/17/wKiom1Ww742iooDjAAL0h0b2d24571.jpg "style=" float: none; "title=" Site2.png "alt=" Wkiom1ww742ioodjaal0h0b2d24571.jpg "/>

You can see that the Python boot process needs to modify the default encoding, then the method will be deleted, when our program needs to manually change the code, we need to reload back

Import sysreload (SYS) sys.setdefaultencoding (' UTF8 ')


Two. Package

1. The module is located in the directory as a package, the directory name is the package name

Example of the package creation process:

Create a new directory Bao, the directory contains 3 files a.py, b.py, __init__.py, note that in this directory must create a __init__.py file, the role of the package is loaded when the first execution of this file, or the package will be quoted error, The contents of the a.py and b.py files are as follows:

#a. Pydef say ():p rint "This is a!" #b. Pydef say ():p rint "This is b!"

Create the test.py file in the Bao sibling directory, as follows:

#方法一: From Bao import afrom bao import Ba.say () B.say () #方法二: Import bao.aimport bao.bfrom bao import *a.say () B.say () #方法三: Fro M Bao Import *a.say () B.say ()

Run test.py Print Results:

>>>

This is a!

This is b!

>>>

Note: With method one and method two o'clock, the __init__.py file is empty and the method three o'clock needs to write the following in __init__.py

#__init__. py__all__=[' A ', ' B ']


2. The module in the bread contains only methods, so let's take a look at how the modules contain classes when they are introduced

Create a new directory Bao2, the directory contains 3 files a.py, b.py, __init__.py,a.py, and b.py file contents as follows:

#a. Pyclass A: @staticmethod def say ():p rint "This is a!" #b. Pyclass B: @staticmethod def say ():p rint "This is b!"

Create the test2.py file in the Bao2 sibling directory, as follows:

#方法一: From bao2.a import afrom bao2.b import Ba.say () B.say () #方法二: from Bao2 import *a.say () B.say ()

Run test2.py Print Results:

>>>

This is a!

This is b!

>>>

Note: For a moment, the __init__.py file is empty, with method two o'clock you need to write the following in __init__.py

#__init__. Pyfrom bao2.a Import afrom bao2.b import b


3. The module is queried first from the current directory, and if no one is queried by the path order

For example: directory structure is bao1/bao2/bao3/a.py

Then: from bao1.bao2.bao3.a Import a

This article from "Today's efforts, tomorrow's success!" "Blog, be sure to keep this provenance http://zhzhgo.blog.51cto.com/10497096/1677773

Python Modules and Packages

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.