Taken https://www.jb51.net/article/149624.htm
The concept of package python with c ++ namespace in very similar large-scale projects in development, several developers are content to use the same function name, in order to avoid problems caused by the same function name, it introduced the concept of the package.
Watching a program written by someone else often seen in the form "from xx import yy" xx is the package
Making a package for local use
Create a folder named dl, the folder name "dl" is our package name, create a new folder inside the "__init __. Py" (note the left and right are two init bottom line symbols), add in the folder own modules to, the following code directory, a.py stored test_a function, b.py function stored test_b
Add To test package at the same level directory dl, and tested
Test code is as follows:
1
2
3
4
|
<code
class
=
"language-python"
>
from
dl
import
a,b
a.test_a()
b.test_b()<
/
code>
|
Can be found in the normal execution module package dl and a function module b is (can be nested package, the package can add package)
_init__.py can control the current package has a module which can be exported, if __init__.py in what are not, that the current package all of the modules can be exported, other modules can be "from xx import *" ways to use the package.
Add the __init__.py "__all__ = [ 'a', 'b']" indicates the current only a, b two modules can be derived encapsulated
Production and installation package
Setup.py created in the directory at the same level in the package dl
1
2
|
<code
class
=
"language-python"
>
from
distutils.core
import
setup
setup(name
=
"testModule"
, version
=
"18.0.4"
, description
=
"test to install module"
, author
=
"szfhy"
, py_modules
=
[
'dl.a'
,
'dl.b'
,
'dl.network.facenet'
])<
/
code>
|
Production package execution command python setup.py build
Generating a compressed package python setup.py sdist
Decompressed packet, and install sudo python setup.py install
Python into the terminal test:
Function as usual. We're done!
Above this python create custom packages and installation to a system of small series is to share the entire contents of everyone, and I hope to give you a reference, I hope you will support script home.