Packages are used to merge a set of modules into a directory, which is a package, and the directory name is the package name
1. The package is a hierarchical file directory structure that defines a Python application execution environment consisting of modules and sub-packages
2. Based on the package, Python can specify the module's import path when executing the module import
Import directory name. Directory name. Module name
Import Dir1. Dir2. mod1
example, the conditions for building the package:
See if/opt/python361 is in the search path
In []: sys.pathout[28]: [',...... '/root/.ipython ', '/opt/python361 ']
Create a new folder under this directory Pkg1
[Email protected] python361]# ls__pycache__ mymod.py[[email protected] python361]# mkdir PKG1
A minimum of 2 files should be included in the PKG1 directory:
__init__.py (__init__.py can contain Python code, but is usually empty, used only to act as hooks for package initialization, to generate module namespaces for directories, and to implement the from * behavior when using catalog import)
mod1.py (a module file)
[[Email protected] python361]# CD Pkg1/[[email protected] pkg1]# touch __init__.py[[email protected]kurol pkg1]# cd-/opt/ Python361[[email protected] python361]# ls__pycache__ mymod.py pkg1[[email protected] python361]# cp mymod.py pkg1/[[E Mail protected] python361]# CD Pkg1/[[email protected] pkg1]# ls__init__.py __pycache__ mymod.py[[email protected] pkg1] # MV Mymod.py mymmm.py
To import a package:
in [[]: Import Pkg1.mymmmin [34]:
This article is from the "Shei" blog, make sure to keep this source http://kurolz.blog.51cto.com/11433546/1935029
Python Learning notes-Package