Python tour. Fourth. Modules and Packages 4.02

Source: Internet
Author: User

First, the use of the module import

1 What is a module?
The module is a set of functions of a system, in Python, a py file is a module, such as module.py, where module name modules

2 using Modules

2.1 Import Module
Three things happen in the first import module
1. Create a namespace for a module
2, the execution module corresponding file, will produce the name in 1 the name space
3, in the current execution file to get a module name, the module name points to 1 of the namespace

Import spam
Emphasis: Subsequent imports directly refer to the results of the first import and do not repeat the file

Import spam
Import spam
Import spam
Import spam
Import spam #以上导入的是第一次导入的结果

Print (spam)


The execution of the function in the module always takes the module's own namespace as the standard
read1=111111
Print (SPAM.READ1) #调用了spam模块中read1函数, the output is read1 in the spam module
money=1111111111111111
Spam.change () #调用了spam模块中change函数, function function modifies the value of money, the last modification of the value in the spam module, regardless of the execution of money in the file

Spam.read1 ()

3. Alias for module
Import spam as SM #将模块名spam改为sm, call the function in the spam module, call on the SM can be used, mainly for some module name too long, instead of shorthand, more convenient to call
Print (Sm.money)
Sm.read1 ()

Engine=input (' >>: '). Strip () #根据输入判断要用什么模块, the module names are changed to the same
if engine = = ' MySQL ':
Import MySQL as db
elif engine = = ' Oracle ':
Import Oracle as DB
Db.parse () #调用时根据输入的模块名来执行



Import Spam,mysql,oracle

Recommended for multiple lines
Import spam
Import MySQL
Import Orcacle

Second, the use of the module From...import ...

 three things occurred in the first import module 
1, create a module namespace
2, execute the module corresponding file, the resulting name is stored in the namespace in 1
prompt: From ... Import: Identical to the first two things of import
3, the name in the module directly in the current namespace, can be used directly, without any prefix

import spam # spam. Name #调用功能方法: module name. function name

from Spam Import Money,read1,read2,change #用drom ... Import ... Call the module, write the feature name in advance so that you don't need to write a name when calling the function you want
print (money)
Read1 ()
Read2 ()
The Change ()
—————————————————————— ——————————————————————————————————————————
Import spam
Print (money)

Note:
1, same import, perform functions in the module, Always use the namespace of the module as the
money=1111111111
Spam.change () #调用spam模块中的change功能 to replace the value of money with 0
Print (money) # The value of the output discovery money is 111111111111, which means that the value of money in the module is changed only in the module function
——————————————————————————————————————————————————————— —————————
2, from ... import name, get the name can be used without prefix direct use, more convenient to use
when the problem is easy to conflict with the same name in the current execution file
money=1111111111111111
Print (Money) #先从当前开始找money, in the current found on the execution of the current money, not found in the module to find, all, do not put the variable name definition is the same as the function in the module
read1=1111111
Read1 ()
————————————————————————————————————————————————
#如果功能太多的话, write not very realistic, generally use * instead of all functions
#注意, because some features are useful, some features are not commonly used, all module designers use __all__ to put the common functions in it, * can only invoke common functions
__all__=[' money ', ' Read1 ') # from: Import *
From ... import *
From spam import * #调用spam模块内所有可以被 * function called

Print (Money)
Print (READ1)
Print (READ2)
Print (change)

Third, the search path of the module

Import M1
M1.F1 ()
The order in which the modules are looked up is:
1. Modules that have been loaded in memory
2. Built-in module
3. Modules included in the Sys.path path
Import Time #调用模块time

Import M1 #调用ml
M1.F1 ()

Time.sleep (#用15秒时间删除ml) , try the following ML to see if you can use
Import M1 #还可以使用, because the first call to the module ML is already in memory, as long as the program does not end, in-memory module ML will not be emptied, you can always call
M1.F1 ()


Import Sys
Print (' time ' in Sys.modules) #Sys.module the imported package #结果为False because the time module has not been imported

Time.sleep (2)
Print (' time ' in Sys.modules) #结果为True because the time module was imported

Sys.path.append (R ' D:\code\SH_fullstack_s1\day14\dir1 ')#将文件地址加入sys. Path
Import M1
M1.F1 ()


Stressing the emphasis placed on emphasizing the emphasis on emphasizing the emphasis on emphasizing
The first path to Sys.path is the folder where the current execution file resides

Python tour. Fourth. Modules and Packages 4.02

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.