I learned the Python list and wrote a function to output the list. This time, I will continue to learn how to share the code we have compiled with the PyPI community, the print_lol function written in the above article is used as an example.
To share the print_lol function, we first need to encapsulate this function into a module. The so-called module is actually an object. the file at the end of py. Here we put the print_lol function in the previous article into a wukong. in The py file (this file is placed in the D: \ python folder), the following is wukong. the content of the py file:
def print_lol(movies):for item_1 in movies:if isinstance(item_1,list):print_lol(item_1)else:print(item_1)
Of course, we can also add some comments to this function so that we can clearly understand the function. In Python, the comments are represented by three ", as shown below:
"Here is the wukong module. The print_lol function is used to output a list." def print_lol (movies): for item_1 in movies: if isinstance (item_1, list ): print_lol (item_1) else: print (item_1)
Then we can press F5 In the python IDLE to restart the Python Shell. This is to load the module we have defined. We can directly print the list using this module, as shown below:
>>> ================================ RESTART ================================>>> >>> movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam",91, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]], "The Life of Brain",1979,"Terry Jones",94, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]]]>>> print_lol(movies)The Holy Grail1975Terry Jones & Terry Gilliam91Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry JonesThe Life of Brain1979Terry Jones94Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry Jones>>>
Here we can see the RESTART text, indicating that the Python Shell has been restarted. This also indicates that our module has been loaded, so we didn't define the print_lol function before movies printing, but we can use it directly.
Prepare for release now we can prepare for the current wukong. py is released to PyPI, but now we have to do a few work: 1. Set wukong. py is put into a folder. Here I put it in the D: \ python \ wukong folder. 2. Create a folder named setup in the new folder. in the D: \ python \ wukong folder, create a setup. py file, which is used to store published data. The content is as follows:
"Import setup function from python publishing tool" from distutils. core import setup "sets the parameters of the setup function. py_modules is the module to be released. Here is wukong. py, so the value is wukong "" setup (name = 'wukong ', version = '1. 0.0 ', py_modules = ['wukong'], author = 'wukongcode', author_email = 'bjwangzhen @ pku.edu.cn ', url = 'HTTP: // blog.csdn.net/wukongcode ', description = 'a simple printer of nested lists ',)
3. Create a publishing file and open a terminal in the D: \ python \ wukong folder. Here it is DOS. Type A line of command: python setup. py sdist, the premise is that we need to configure the python environment variables, as follows:
Open DOS and enter the following statement:
4. install the release to your local Python copy, and enter the command: python setup. py install on the terminal, as shown below:
Now we release the module to the local database, so we can also import the local database in shell and use the following:
>>> import wukong>>> names = ['zhangsan','lisi','wangwu']>>> print_lol(names)Traceback (most recent call last): File "
", line 1, in
print_lol(names)NameError: name 'print_lol' is not defined>>>
We used import wukong to import our modules, but an error was reported during use. Print_lol is not found because there is a namespace concept in Python. By default, it is in the namespace of _ main, our code is in the wukong namespace, so we need to specify the corresponding namespace here, as shown below:
>>> import wukong>>> names = ['zhangsan','lisi','wangwu']>>> wukong.print_lol(names)zhangsanlisiwangwu>>>
Here we use wukong. print_lol to call this method. Now we have prepared the release module. If we want to release our module to PyPI, we also need to register an account on PyPI.
To register a PyPI website and release our module, you must first register your account on the PyPI website (click to open the link:
After the above information is filled in, the PyPI website will send us an email with a confirmation link. We can click to confirm registration.
To upload code to PyPI, first fill in the information we registered on PyPI in DOS, which only needs to be executed once on the local machine. As follows:
After registering our own information, we can upload our own code. As follows:
Now, we have successfully uploaded our own code to PyPI, so that others can download and use our modules.