One: module
1 in Python, a. py file is called a module
2 Benefits of Python, advantages:
A improves the maintainability of the Code
b When a module is written, it can be referenced elsewhere. When we write programs, we often refer to other modules, including Python built-in modules and modules from third parties.
3 The use of modules also avoids conflicting function names and variable names. Functions and variables of the same name can exist in separate modules. However, it should also be noted that function names and variable names do not conflict with the built-in name as much as possible.
4 to avoid module name collisions, Python introduces a way to organize modules by directory, called packages.
mycompany├─ __init__.py├─ abc.py└─ xyz.py
Once the package is introduced, all modules will not conflict with others as long as the package name in the top layer does not conflict with others. Now, the name of the abc.py
module becomes mycompany.abc
, similarly, xyz.py
the module name becomes mycompany.xyz
.
Each package directory will have a __init__.py
file, this file must exist, otherwise, Python will treat this directory as a normal directory, rather than a package. __init__.py
it can be an empty file, or it can have Python code, because __init__.py
it is a module, and its module mycompan(init左右都是2个下划线)
name is
Class 5, can have a multilevel directory, composed of multi-level package structure. For example, the following directory structure:
mycompany ├─ web │ ├─ __init__.py │ ├─ utils.py │ └─ www.py ├─ __init__.py ├─ abc.py └─ xyz.py
www.py
the module name of the file ismycompany.web.www
6 When you create a module, you should be careful about naming it and not conflict with the module name that comes with Python. For example, the system comes with the SYS module, its own module is not named sys.py, otherwise it will not be able to import the system's own SYS module. It is a good idea to see if the system already exists in the module, the check method is executed in the Python interactive environment import abc
, if successful, it indicates that the system exists this module.
Two: Using the module
1
#!/usr/bin/env python3# -*- coding: utf-8 -*-‘ a test module ‘__author__ = ‘Michael Liao‘
Lines 1th and 2nd are standard comments, and the 1th line of comments allows the hello.py
file to run directly on Unix/linux/mac, and the 2nd line comment indicates that the. py file itself uses standard UTF-8 encoding;
Line 4th is a string representing the document comment of the module, and the first string of any module code is treated as a document comment of the module;
The 6th line uses __author__
variables to write the author in, so that when you open the source code, others will be able to admire your name;
These are the standard file templates for Python modules.
The 2 sys
module has a argv
variable that stores all the parameters of the command line with the list. argv
there is at least one element, because the first parameter is always the name of the. py file.
3 Import Hello Hello.test ()
A Import module: Import module name or import module name as new module name (this method is suitable for the module name is too long, call or use inconvenient rename)
b Import the specified module properties: From Module name Import Property 1,2,3,4 ... Eg:from ch6_1 import Count,print.
4 in a module, we may define many functions and variables, but some functions and variables we want to use for others, some functions and variables we want to use only inside the module. In Python, this is done _
by a prefix.
A normal function and variable name are public, and can be directly referenced, such as: abc
, x123
, PI
etc.;
b A __xxx__
variable like this is a special variable that can be referenced directly, but has a special purpose, such as the above__author__。我们自己的变量一般不要用这种变量名。
C similar _xxx
and __xxx
such functions or variables are non-public (private), should not be directly referenced, such as _abc
, and __abc
so on.
d external functions that do not need to be referenced are all defined as private, and only those functions that need to be referenced externally are defined as public.
5 The search path for the module:
A when we try to load a module, Python searches for the corresponding. py file under the specified path, and if it doesn't, it will get an error.
b By default, the Python interpreter searches the current directory, all installed built-in modules, and third-party modules, and the search path is stored in the sys
variables of the module path
.
>>> import sys>>> sys.path[‘‘, ‘/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip‘, ‘/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6‘, ..., ‘/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages‘]
c
If we want to add our own search directory, there are two ways:
One is to modify directly sys.path
, add the directory to search:
>>> import sys>>> sys.path.append(‘/Users/michael/my_py_scripts‘)
This method is modified at run time and is invalidated after the run is finished.
The second is to set the environment variable PYTHONPATH
, and the content of the environment variable will be automatically added to the module search path. Settings are similar to setting the PATH environment variable. Note that you only need to add your own search path, and Python's own search path will not be affected.
Python Day 7 (1) module