Modules and Packages1 What is a module?
A module is a file that contains the Python definition and declaration, and the filename is the suffix of the module name plus the. py
2 Why do I use modules?
If you go to the Python interpreter and then re-enter it, then the functions or variables you defined previously will be lost, so we usually write the program to a file so that it can be saved permanently and executed in Python test.py when needed, when test.py is called a scripting script
3 How do I use modules?
Instance file: spam.py, file name spam.py, module name spam
Print ("from the spam.py") money=1000 def read1 (): print ("Spam-->read1-->money,money") def read2 (): Print ("spam-->read2-->") read1 () def change (): Global Money money=0
The 3.1.1 module can contain definitions of executable statements and functions that are intended to initialize modules that execute only when the module name encounters the import imports statement for the first time.
Python's optimization means that the module name is loaded into memory after the first import, and the subsequent import statement only adds a reference to the module object that has loaded the large memory, and does not re-execute the statements inside the module
Import spam #只在第一次导入时才执行spam. Py inside code, # The effect is to print only once "from the spam.py" import Spamimport Spamimport Spam execution result: from the spam.py
3.1.2 Each module is a separate namespace that defines the function in this module as the global namespace for the function's namespace.
So when we're writing our own modules, we don't have to worry about the global variables that we define in our own module that will conflict with the consumer's global variables when they are imported.
#测试一: Money does not conflict with Spam.money #test.pyimport spammoney=100print (spam.money) execution result: from the spam.py1000
#测试二: Read1 and Spam.read1 do not conflict #test.pyimport spamdef read1 (): print ("Read1") spam.read1 () execution result: from the spam.pyspam-- >read1-->money,money
#测试三: Global variable that performs the Spam.change () operation money is still spam #test.pyimport spammoney=10spam.change () print (money) execution result: from the Spam.py10
Summary: The import module executes the file
First thing: Create a namespace to hold the name defined in the spam.py
Second thing: Execute spam.py based on the namespace you just created
The third thing: Create a name spam point to the namespace, spam. The operation of the name, is based on spam.py
3.1.3 alias for module name, equivalent to M1=1;M2=M1
Import spam as smprint (sm.money) execution result: from the spam.py1000
3.1.4 Importing multiple modules in one line
Import Sys,os,re
3.2 from ... import .....
3.2.1 compared to import spam, the name space "spam" of the source file is brought to the current namespace, and must be used as a spam. Name.
The FROM statement is the equivalent of import. A new namespace is created, but the name of the spam is imported directly into the current namespace, and the name is used directly in the current namespace.
From spam import read1,read2
This way, it is good to use READ1 and read2 directly at the current location, while executing, still with the spam.py file global namespace
#测试一: Imported function read1, execution still returns to spam.py for global variables money#test.pyfrom spam import read1money=1000read1 () execution results from the spam.pyspam-- >read1-->money,money
#测试二: The imported function read2, call READ1 () to execute, and still return to spam.py for read () #test. Pyfrom spam import read2read2 () execution result from the spam.pyspam-- >read2-->spam-->read1-->money,money
If there is currently duplicate name Read1 or read2, then there will be coverage effect
#测试三: The imported function read1 is overwritten by the read1 defined by the current location #test.pyfrom spam import Read1def read1 (): print ("----->") read1 () execution result from The spam.py----->
One particular point to emphasize is that variable assignment in Python is not a storage operation, but a binding relationship
From spam import money,read1money=100print (money) Read1 () Executes the result: from the Spam.py100spam-->read1-->money,money
3.2.2 also supports as
Form spam Import read1 as read
3.2.3 support for importing multiple lines
From spam import (Read1, read2, Money )
3.2.4 from spam import * with __all__ * control to use
Add a line to the spam.py
_all_=["Money", "read1"] #字符串形式
#test. Pyfrom Spam Import * #不建议去用 to avoid duplicate name print (READ1) execution results from the spam.py1000<function read1 at 0x02543390 >
3.3 Module as script execution
We look at the module name through the global variable __name__ of the module
Run as script:
__name__ equals "__main__"
Import as module:
__name__
Print (__name__) #test. Pyimport Spam execution results spam
Function: Used to control the. py file to perform different logic in different scenarios
if __name__ = = ' __main__ ': print ("file as script to execute") executes the result file as a script to execute
3.4 Module Search Path
The order in which the nodes are found is: modules that are loaded in memory, built-in modules->sys.path paths
import spam #先找内存: sys.modules-----"Look for the built-in-----" finally find Sys.path
After initialization, the Python program can modify the Sys.path path before placing it in precedence over the standard library being loaded
Import syssys.path.append (R ' f:\\python_fullstack_s4\\day35\\ module ') sys.path.insert (0,r ' f:\\python_fullstack_s4\\ day35\\ module ') #排在前面的目录, preferred to be searched
python--Modules and Packages 1