Small knowledge, big challenge! This article is participating in the “Essentials for Programmers” creative activity. This article has participated in the “Digitalstar Project” to win a creative gift package and challenge the creative incentive money.
Official Python column # 9, stop! Don’t miss this zero-based article!
The previous committee’s Python beginner to Master column has accumulated eight Python articles, and of course the committee’s blog has dozens of articles on Python applications.
Showed a lot of ah, along with the friends estimated to learn a lot.
In a few minutes, I’m going to give you a little bit of a guide to developing a Python library. Next time I’ll show you how to develop generators!
(Pilot description, the following figure of contact groups/contact methods are demo display, do not search! Don’t pay attention! Ha, ha, ha)
What is a Python library?
A Python library is simply a collection of Python script code. This library can include one or more Python code files. The point is to provide unified portals (modules) for others to use.
Small white if not clear, it is understood as a mathematical formula.
The process by which we use formulas to calculate the results of data can be analogous to using python libraries to call and run the results.
Now develop a library
I’ve written a small and complete example of a Python project before, but it’s still too many elements for a relative novice.
The first step is the basic configuration of the project
Required file: readme.md, which tells other developers about the code you’re sharing and how to use it.
A typical project README file would look like this:
Generate what’s above and copy what’s hereREADME.mdmodified
The point is:
- Project introduction necessary
- PIP install XXX (XXX is your library name)
- Simple instructions for use
Other information like joining community groups can be added.
The second step is to develop the code in the module
In Python, it’s easy to create a directory and place an __init__.py init to mark the current directory as available.
But let’s not be so reckless.
- Create a pypi_seed
- Then place the __init__.py file inside
- Finally, write main.py (this is the core file of the library, also called core.py)
The effect is as follows:
Step three, enrich the core code
The above just created some empty files.
Now let’s open main.py and copy and paste the following content.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Time: 2021/9/5 12:07 am
# @Author : LeiXueWei
# @csDN /Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : main.py
# @Project : pypi_seed
def main():
print("Pypi_seed Project")
print("Welcome to follow the public account [Lei Xuewei] [Gu Cold] [Bu Xiaochan] and join the Python developer camp!")
print("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =")
print("How to create a project:")
print("1) Open the Python terminal REPL:)
print("2) Enter the following code:)
print("import pypi_seed.main as pm")
print("Pm.generate () # generate pypi_sample from current directory")
print("# or add a path parameter")
print("PM. Generate ('/ TMP ') # generate/TMP /pypi_sample)
print("# or specify more project information:")
print(Generate (path=\".\", project=\"pypi_sample\", author=\"pypi_seed\") # generate(path=\".\", project=\"pypi_sample\", author=\"pypi_seed\") # generate(path=\".)
print("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =")
print("Please feel free to contact us ": https://jq.qq.com/?_wv=1027&k=ISjeG32x)
print("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =")
if __name__ == '__main__':
main()
Copy the code
Code parsing:
This implements a main function that outputs some program information. For example, some virtual accounts and contact information, strongly prompt readers do not open links or search groups! (Student committee here specially marked once! For demo purposes only)
There are a lot of things you can do, but this article will start simple and develop generators later!
Well, development is over.
Finally call run to see what happens
You can print some developer community information in the library prompt,Readers are requested not to search.
In addition, other people calling your library look like this:
import pypi_seed.main # Import the main program of your library
main.main() Call main
Copy the code
conclusion
The whole process was pretty simple.
It’s not easy for others to find your library, so make your README instructions as simple as possible!
Then there are examples of use, which must be concise and easy to understand.
If you like Python, please pay attention to learn the Basic Python column or Get started in Python to master the big column
Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Creation is not easy, please pay attention to the collection of likes, or leave a comment to encourage!