Package a Python library (PyPa)

Writing environment for this article, like this dark + dim light

Origin~Why is there such an article? Because I recently read Python's official documentation, which happens to read about installing Python modules here, and then I read the source code the next day, which is also supported by some practice. So take a note.

All this is an organization that maintains:

The Python Packaging Authority (PyPA) is a working group responsible for maintaining a core set of software projects used in Python packaging.

Here I find a library for instructions:

Use this

Then unzip it, see it, say it today

Here is the version of Print Python

Reference location for my article

py -m pip --version

pip 21.3.1 from C:\Users\yunswj\AppData\Local\Programs\Python\Python36\lib\site-packages\pip (python 3.6)

Print the version of the PIP.

Look at this
py -m pip install --upgrade pip

Source file look like pip

└── src/
    └── example_package/

Future libraries should write like this

Let's create one

pyproject.toml tells build tools, such as pip and build, what they need to build a project.

Give dependent packages to build packages

Our manim, also written here

Configuration Metadata

There are two types of metadata: static and dynamic.

  • Static metadata (setup.cfg): Make sure it's the same every time. This is simpler, easier to read, and avoids many common errors, such as coding errors.
  • Dynamic metadata ( may be indeterminate. Any dynamic or defined project, as well as extensions to extensions or setuptools, needs to enter

setup.cfg should prefer static metadata (). Dynamic metadata ( should only be used as an escape hatch if absolutely necessary. was necessary in the past, but can be omitted in newer versions of setuptools and pip s.

This is an example of demo:

name = example-pkg-YOUR-USERNAME-HERE
version = 0.0.1
author = Example Author
author_email =
description = A small example package
long_description = file:
long_description_content_type = text/markdown
url =
project_urls =
    Bug Tracker =
classifiers =
    Programming Language :: Python :: 3
    License :: OSI Approved :: MIT License
    Operating System :: OS Independent

package_dir =
    = src
packages = find:
python_requires = >=3.6

where = src

Examples of setuptools:

name = my_package
version = attr: src.VERSION
description = My package description
long_description = file: README.rst, CHANGELOG.rst, LICENSE.rst
keywords = one, two
license = BSD 3-Clause License
classifiers =
    Framework :: Django
    License :: OSI Approved :: BSD License
    Programming Language :: Python :: 3
    Programming Language :: Python :: 3.5

zip_safe = False
include_package_data = True
packages = find:
scripts =
install_requires =
    importlib; python_version == "2.6"

* = *.txt, *.rst
hello = *.msg

console_scripts =
    executable-name = package.module:function

pdf = ReportLab>=1.2; RXP
rest = docutils>=0.3; pack ==1.1, ==1.3

exclude =

Equivalent to textbook-level writing

Copy our metafile in

This is the syntax, the same configuration file as the win ini file

Supported INI file structure

The configuration file consists of sections, each guided by a [section] header followed by key/value entries separated by a specific string (=or: 1 by default). By default, some names are case sensitive, but the key is not 1. Remove leading and trailing spaces from keys and values. If the parser is configured to allow it to be 1, the value can be omitted, in which case the key/value separator can also be omitted. Values can also span multiple lines, as long as they are indented deeper than the first line of the value. Depending on the parser's pattern, empty rows may be considered part of a multiline value or ignored.

Probably this is the case

Address of the build tool

Error after execution, I checked that it should be built in a virtual environment

There is a discussion in this article

python.exe -m venv C:\Users\yunswj\Desktop\tf   

But I won't use it

pip install virtualenv

Install this tool first

python3 -m venv .

Create a virtual environment in the current environment

Then activate it

The new environment has just two packages

py -m pip install --upgrade build

Install build tools

py -m build

I failed to build

Deactivate the environment, delete it
 Reference Article

Create virtual environment in current directory

$ python3 -m venv .

Create a separate python environment in the current directory

$ virtualenv --no-site-packages venv

Activate virtual environment

$ source venv/bin/activate

Deactivate virtual environment

$ deactivate

Delete virtual environment

$ rm -rf venv

Operations, once you get started, a bunch of errors, mb. You're when I build it.

Normally such a directory is a good package

A good package must have no less than one document, so install it below

py -m pip install -U sphinx

After installation


Perform this

Press as prompted


And it's built

More details here

Note that venv is the latest way to build

If you do, you have the option to choose an interpreter

Posted by Cultureshock on Wed, 01 Dec 2021 19:34:13 -0800