Modules (module)
Python has a concept called module, which is similar to a header file in C and a package in Java, such as in Python to invoke the SQRT function, which must be introduced into the math module with the Import keyword. In layman's terms, a module is like a toolkit, and if you want to use the tool in the toolkit (like a function), you need to import the module.
Import Module
- Import: Introduce a specific module, you can introduce multiple modules at once, separated by commas can
Eg:import modlue1,module2,module3,..... Modulen, when using the function in the module, the format is: Modulename.funname ()
- From ...: Import a specified part from a module into the current namespace
Eg: from moddulename import name1 [, Name2 [, Name3 [,.... Namen]]
- From ... import *: All contents of a module are imported into the current namespace
Eg: from modulename import *
Package
The package organizes the associated modules together, effectively avoiding the problem of module name conflict, and makes the application organization structure clearer.
A package can contain multiple modules.
Let's assume that our package example has the following directory structure:
Phone/ __init__.py common_util.py Voicedta/ __init__.py Pots.py Isdn.py Fax/ __init__.py G3.py Mobile/ __init__.py Analog.py igital.py Pager/ __init__.py Numeric.py
The Phone is the topmost package, VOICEDTA, etc. is its child package. We can import the child package like this:
import Phone.Mobile.AnalogPhone.Mobile.Analog.dial()
You can also use From-import to implement different requirements of the import
Module production
The function method file is defined first, and several function methods are defined in the file to be available to the outside world.
There is usually a file in the Moudle __init__.py
. Some __init__.py
of them are blank, others have __all__
parameters.
If the other page __init__.py
is blank when import, you can import all the functions directly into Moudle. If it is __init__.py
defined __all__
, import only imports some of the __all__
defined content.
Module release
- Directory structure of the module:
├── setup.py├── suba│ ├── aa.py│ ├── bb.py│ └── __init__.py└── subb ├── cc.py ├── dd.py └── __init__.py
The directory structure of the module contains a setup.py file that defines module information such as the module name, the included module, and so on.
fromDistutils.coreImportSetupsetup (Name="Module Name", version="version", description="Module Description", author="Module Author", py_modules=['Suba.aa','suba.bb','subb.cc','subb.dd' (here is mainly the contents of the module included)])
Linux命令(当前位置位于模块文件夹):
python setup.py build
构建后目录结构.├── build│ └── lib.linux-i686-2.7│ ├── suba│ │ ├── aa.py│ │ ├── bb.py│ │ └── __init__.py│ └── subb│ ├── cc.py│ ├── dd.py│ └── __init__.py├── setup.py├── suba│ ├── aa.py│ ├── bb.py│ └── __init__.py└── subb ├── cc.py ├── dd.py └── __init__.py
Linux命令(当前位置位于模块文件夹):
python setup.py sdist打包后,生成最终发布压缩包 moduleName-version.tar.gz , 目录结构.├── build│ └── lib.linux-i686-2.7│ ├── suba│ │ ├── aa.py│ │ ├── bb.py│ │ └── __init__.py│ └── subb│ ├── cc.py│ ├── dd.py│ └── __init__.py├── dist│ └── xwp-1.0.tar.gz├── MANIFEST├── setup.py├── suba│ ├── aa.py│ ├── bb.py│ └── __init__.py└── subb ├── cc.py ├── dd.py └── __init__.py
模块安装
- Find the module's compression package
- Extract
- Go to Folder
- Execute command sudo
python setup.py install
At this point, the module has been installed in the system, you can import the module directly using the Import keyword.
PS: If you perform a directory installation at install time, you can use thepython setup.py install --prefix=安装路径
The release and installation of modules in Python