¶Preface
In Python, we often install various third-party libraries by pip cmd from PyPI, so how can we build a standard python library from scratch? Then, Let’s find it out.
¶Project packaging
We need to define a standard Python project structure, and then start packaging step by step according to the following steps. [1]
¶Project structure
Start by creating a code folder to publish, organizing the project structure and adding a few necessary files, such as Project Name, LICENSE, README.md, setup.py and so on. [2]
1 | packaging |
The example project is simple, all of them are empty files, and the other files are as follows:
1/ hello.py
1 | def hello_world(): |
2/ bye.py
1 | def goodbye(): |
3/ utils.py
1 | def test(): |
¶Add LICENSE
PyPI requires that all uploaded packages must contain a license, take advantage of [3] help select a license, and then copy the license content into a file LICENSE.
Note that some licenses require a year ([year]) and owner ([fullname] or [name of copyright owner])
FOR EXAMPLE, AN MIT LICENSE:
1 | MIT License |
¶Add description
Write a detailed description of the project according to the markdown syntax, which can then be used as an entry in setup.py 「long_description」
1 | Example Package |
¶Configure setup metadata
There are two types of setup metadata types:
- static setup metadata
- dynamic setup metadata
The official recommendation is preferred for static metadata setup.cfg or setup.py.
¶Static setup metadata
The following is an official example setup.cfg.
1 | [metadata] |
field | Description |
---|---|
name | if you are publishing on pypi.org, the name must be unique and can consist only of english letters, ,_- |
author/email | Identifies the author |
description | A short introduction to the package |
long_description | A detailed description of the package, which can specify a description file |
url | The project home page |
project_urls | Additional links related to the project |
classifiers | additional metadata, such as license, compatibility. see https://pypi.org/classifiers/ for a full list |
¶Dynamic setup metadata
The official example, as can be seen, is essentially the same and can also be written using a ready-made template setup.py as [4]
1 | import setuptools |
In fact, it can be understood as an ini file that contains the default options of the command with setup.cfg or setup.py
¶Prepare venv
We also have Ubuntu 20.04 Server (Python 3.8.10) in Win10 WSL-2, creates and uses virtual environments 「venv」
1 | install venv |
¶Build package
Use the following configuration file setup.py
1 | import setuptools |
Install setuptools and wheel which support building packages from source code. [5]
1 | pip install setuptools |
Go to the packaging folder, check setup.py, and if there is an error, a prompt message will be printed.
1 | running check |
Packaging to .tar.gz or wheel, automatic creation of directories, and corresponding file sdist. [6]
1 | build to .tar.gz |
¶PyPI Published
So far, we have completed the packaging of the project. Next, we need to publish the packaging module to pypi or our own private server.
¶Official PyPI server
When we use official PyPI server, We need to register an account first in [7]
Then to install twine to upload packages.
1 | pip install twine |
Next to input account info and finish upload.
1 | twine upload dist/* |
¶Local PyPI server
Install and create a folder to place the published package pypiserver packages. [8]
1 | pip install pypiserver |
Upload to directory, mv operate directly on the same virtual machine my_project-0.0.1.tar.gz to ~/packages.
1 | mv dist/my_project-0.0.1.tar.gz ~/packages |
Run the server, the port specified as 8080, listens to all ip addresses by default 8080.
1 | pypi-server -p 8080 ~/packages & |
¶Test
Search for packages on the local server my_project or PyPI
1 | official server |
Install and use the package my_project.
1 | install from official server |
¶Reference
[1] https://packaging.python.org/en/latest/tutorials/packaging-projects/
[2] https://pythonguidecn.readthedocs.io/zh/latest/writing/structure.html
[3] https://choosealicense.com/
[4] https://github.com/absentm/akparse/blob/main/setup.py
[5] https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/
[6] https://zhuanlan.zhihu.com/p/161930778
[7] https://pypi.org/account/register/
[8] https://github.com/pypiserver/pypiserver