Description
While using Python, we may need to use some other functions that others have written in addition to the built-in functions. Or the code we write is also expected to be available to others. To implement such a function, we need to define our own modules as follows:
STEP1: Create a new py file
First we save the function of the printed list written in the previous chapter as a. py file, assuming I save it here as kider.py.
1 """2 This is a printed list of functions, if there are nested lists in the list will also expand to print out3 """4 defprint_list (Movies):5 ifisinstance (Movies, list):6 forMovieinchMovies:7 print_list (Movie)8 Else:9 Print(Movies)
kider.py
If you want to publish it, it's a good idea to add a comment to the function code quickly. In Python, the string table comment in the three-quote "" ", this is a multiline comment, and the single-line comment uses #.
In this case, if you want to use the module, the most primitive way is to put the previous kider.py file into the native Python module address.
How to know where the native Python module is placed, simply enter it in the idle (Python console):
Import Sys
Sys.path
The output of the Sys.path is the file where the native Python module resides
1>>>ImportSYS2>>>Sys.path3["','/usr/local/lib/python3.5/dist-packages/redis-2.10.5-py3.5.egg','/usr/lib/python35.zip','/usr/lib/python3.5','/usr/lib/python3.5/plat-x86_64-linux-gnu','/usr/lib/python3.5/lib-dynload','/home/zyq/.local/lib/python3.5/site-packages','/usr/local/lib/python3.5/dist-packages','/usr/lib/python3/dist-packages']
Modulepath
STEP2: Installing the Py file as a module to a local
But in fact, we do not need to put their own Python module into the native Python module address, you can directly through the command to install the Python module to the machine:
The main steps are as follows:
1. Create a new directory with the same name as the Python file you want to install, named Kider, and put the previous kider.py file in that directory
"2. A setup.py file is created in this directory, and the contents of the edited file are as follows:
1 fromDistutils.coreImportSetup2 Setup (3Name ='Kider',4Version ='1.0.0',5Py_modules = ['Kider'],6Author ='Kide',7Author_email ='[email protected]',8URL ='http://www.rampage.com',9Description ='kide\ ' s first Test module',Ten)
setup.py
"3. Enter the Kider directory, and use Python3 setup.py sdist to compile, you can find many more files and directories after the compilation is completed.
"4. Use the sudo python3 setup.py install command to mount the local Python module.
"5. Next, you can use the Python module in the program as follows:
1 Import Kider 2 movies = ["haha" "Ha""movie2 "]; 3 kider.print_list (Movies)
Usekider
Why can't I just use the Python module that I recently published with Print_list?
This is because the default namespace in the Python shell is __main__, and if you want to use print_list because he belongs to the Kider namespace, you should take that namespace.
But of course you can also use the same syntax as in setup.py to introduce the methods in the namespace so that you can use the method name directly: Form Kider import print_list
However, there is a risk that the function with the same name will overwrite the current namespace.
PS: Note that after the install actually in the newly generated __pycache__ folder more than a PYc file, the file can be as a compiled class file, can be deleted, but if deleted, in the execution of the time may lose the corresponding optimization performance.
STEP3: Optional upload the module to the PyPI
You can now upload the module to the PyPI (Python package index):
"1. First you need to register an account on the official website http://pypi.python.org
"2. Python3 setup.py Register If the first step is completed, then this step selects option 1, otherwise the department can also choose 2 to create a direct new account registration
"3. Pyhton3 setup.py sdist Upload this step to upload the module code
If you choose to upload the code to the tall PyPI, then you definitely need to improve the function of the original function, add two parameters, one can specify whether indentation is required, one can specify the number of tabs when indentation. The final version of the code is as follows:
1 """This is kide ' s python module"""2 3 """This function prints the parameters passed in, and if the passed in parameter is a list, the items in the list are printed sequentially. 4 supports multiple nested lists, which sequentially print out all the subkeys in each nested list. 5 """6 #The first parameter is the object that the input prepares for output, and the second parameter indicates if the child list in the object is indented, and the third parameter indicates the number of tab indents if indented7 defPrint_list (args, level=0, indent=False):8 ifisinstance (args, list):9 forArginchargs:TenPrint_list (ARG, level+1, indent) One Else: A ifindent: - forTab_stopinchRange (level): - Print("\ t", end="') the Print(args)
kider.py
Parameters that specify a default value for Python can be omitted, such as the ability to specify a third parameter directly without specifying the value of the second parameter, at which point the call is made as follows:
1 Import Kider 2 >>> kider.print_list (['aa'bb' cc'], indent=True)3 aa4 BB5 cc
Invoke
Python (2): Creating a function Module