python custom package production installation to a system and method

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

python custom package and installation to a system

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

python custom package and installation to a system

Generating a compressed package python setup.py sdist

python custom package and installation to a system

Decompressed packet, and install sudo python setup.py install

Python into the terminal test:

python custom package and installation to a system

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.

Guess you like

Origin www.cnblogs.com/fyly/p/11074441.html