Python advanced 03 Module

Source: Internet
Author: User

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

 

We have seen functions and objects before. Essentially, they are all for better organizations.ProgramTo facilitate reuse.

Module)For the same purpose. In python,A. py file forms a module.. Through the module, you canCall programs in other files.

 

1. import and use modules

First, write a first. py file with the following content:

DefLaugh ():Print 'Hahahaha'

Write another second. py

 
ImportFirstForIInRange (10): First. Laugh ()

In second. py, we didn't define the laugh function, but by introducing (import) from first, we can directly use the laugh function in first. py.

 

We can see from the above that after the module is introduced, we canModule. ObjectTo call the desired object. In the above example, the first is the introduced module, and laugh () is the object we introduce.

In addition, there are other import methods, such as import a as B, from a import *, which are all reasons for convenient writing, essentially no difference.

 

2. Search Path

Python will search for the module it is looking for in the following path:

1. folder of the program

2. installation path of the standard library

3. Path included in the operating system environment variable pythonpath

 

If you have a custom module or a downloaded module, you can put it in the corresponding path as needed so that python can find it.

 

3. Module package

You can place modules with similar functions in the same folder (such as DIR ).

 
ImportDir. Module

.

 

Note: This folder must contain_ Init _. pyTo remind Python that the folder is a module package. _ Init _. py can be an empty file.

 

Summary

Import Module

Module. Object

_ Init _. py

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.