Import
Print (' Frrom the my_module.py ')
money=1000
Def rend1 ():
Print (' My_my_module->reand1->money ', money)
Def rend2 ():
Print (' My_module->read2 calling Read1)
Read1 ()
def change ():
Global money
Money=0
A module can contain definitions of executable statements and functions that are intended to initialize the module
Only the first time the module name is encountered importing import statements is performed
(Import statements can be used anywhere in the program, and for the same yige
module and import multiple times, in order to prevent you from repeating the import, is
Python optimization means: The first import is loaded into memory
The subsequent import only adds a single reference)
Alias for module name, equivalent to M1=1;M2=M1
1 Import my_module as SM2 print (Sm.money
#mysql. py
Def sqlparse ():
Print (' from MySQL sqlparse ')
#oracle. py
Def sqlparse ():
Print (' from Oracle Sqlparse ')
#test. py
Db_type=input (' >>: ')
if Db_type = = ' MySQL ':
Import MySQL as db
elif Db_type = = ' Oracle ':
Import Oracle as DB
Db.sqlparse ()
Package
A package is a way to organize the Python module namespace by using the '. Module name '.
1. Whether it is an import or From...import form, it is the first time to be alert when you encounter a dot in an imported statement (rather than in use): This is the import syntax for packages
2. Packages are directory-level (folder-level), folders are used to form a py file (the essence of the package is a directory containing __init__.py files)
3. Import the file, the resulting namespace name from the file, import package, the name of the resulting namespace is also derived from the file, that is, the __init__.py under the package, the import package is the essence of the import of the file
Emphasize:
1. In Python3, even if there is no __init__.py file under the package, import package will still not error, and in Python2, the package must have the file, or import packet error
2. The purpose of creating the package is not to run, but to be imported to use, remember that the package is just a form of the module, the package is a module
1. Import statements related to packages are also divided into imports and from ... import ... Either way, regardless of location, you must follow a principle when importing: any point on the import, the left side must be a package, otherwise illegal. Can carry a series of points, such as Item.subitem.subsubitem, but all must follow this principle.
2. For import, there is no such restriction when used, the left side of the point can be a package, a module, a function, a class (they can all call their own properties in the way of a point).
3. Compare the application scenarios for import item and from item import name:
If we want to use name directly then we must use the latter
There are two SQL modules MySQL and Oracle, depending on the user's input, choose different SQL functions
Python discards modules and packages