5.1 Basics module
-
-
Built-in modules, the functionality provided inside python
Import SYS # Import Module is loaded into memory priority Print ( SYS, the argv)
-
Third-party modules, download, install, use
1. PIP package management tool . The PIP EXE file is located in the directory to environment variable PIP install to install the module name # pip install xxxx 2. Source Installation Download Source Package: compressed files. Unzip the file open cmd window, and enter the directory: cd C: \ Python36 \ Lib \ Site - Packages Standard Package execution: python36 Setup. Py Build execution: python36 Setup. Py install
-
Custom Modules
-
-
New xxx.py file
def f1():
print('f1')
def f2():
print('f2') -
New x1.py file
# Module calls the custom function
# a manner directly into the module, all functions
Import XXX
XXX. F1 ()
XXX. F2 () # Second way to import the specified function from XXX Import F1, F2 ( from XXX Import *) # prevent variable names repeated from xxx Import f1 AS f f1 () -
run
python x1.py
-
-
-
Modules may be a py file as a module for later py file calls other functions of certain aspects of file
-
Calling module package, including: folders, files stored in multiple folders py
If a package is imported, the default bag module can not be used
Introducing a package corresponds to the implementation of
__init__
the content file py-
abc.py file contents
# abc.py
def func():
print(1,2,3)
def show():
print(4,5,6)
print("你好") -
Import module, the module loading all values into memory
Import ABC Print ( . 7, . 8, . 9) function call to the module # ABC. FUNC () # l, 2,3, hello 7,8,9
-
-
The method of introducing the module
-
import
-
import function module 1. Module 1 ()
-
import module 1. Module 1 Module 2 Module 3 Module 2 Module 3 function ()
-
-
from xx import xxx
-
from module import function module function ()
-
from module import function module as ff ()
-
from module 1 function module import * () function 2 ()
-
from module import module module function ()
-
import module from the module as m m. function ()
-
-
Multiple imports only imported once
# Example of an
Import jd # first load: loaded again in all of the content jd.
Import JD # has been loaded by, not loaded.
Print ( 456) # two exemplary Import the importlib Import JD the importlib. reload ( JD) Print ( 456) -
Special case
1 # manner as a function of all imported modules 2 import ABC . 3 abc.func () . 4 . 5 # second approach . 6 from ABC import FUNC # # from import function module . 7 from ABC import Show . 8 from ABC import FUNC, Show # from module import function / function () . 9 from ABC import * 10 . 11 from ABC import FUNC F AS # import function module from an alias as an alias ()
-
-
to sum up
-
And py module file to be executed in the same directory and requires a lot of features in the module, recommended: import module
-
Other Recommendations:. From module import module / module function ()
-
Other Recommendations:. From module import function module function ()
-