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:

https://www.pypa.io/en/latest/

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:

https://github.com/3b1b/manim

Use this

https://docs.manim.org.cn/
https://3b1b.github.io/manim/

Then unzip it, see it, say it today

Here is the version of Print Python

https://packaging.python.org/

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.

https://packaging.python.org/tutorials/packaging-projects/

Look at this

https://pip.pypa.io/en/stable/
https://setuptools.pypa.io/en/latest/
py -m pip install --upgrade pip

Source file look like pip

packaging_tutorial/
└── src/
    └── example_package/
        ├── __init__.py
        └── example.py

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 (setup.py): may be indeterminate. Any dynamic or defined project, as well as extensions to extensions or setuptools, needs to enter setup.py.

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

This is an example of demo:

[metadata]
name = example-pkg-YOUR-USERNAME-HERE
version = 0.0.1
author = Example Author
author_email = author@example.com
description = A small example package
long_description = file: README.md
long_description_content_type = text/markdown
url = https://github.com/pypa/sampleproject
project_urls =
    Bug Tracker = https://github.com/pypa/sampleproject/issues
classifiers =
    Programming Language :: Python :: 3
    License :: OSI Approved :: MIT License
    Operating System :: OS Independent

[options]
package_dir =
    = src
packages = find:
python_requires = >=3.6

[options.packages.find]
where = src

Examples of setuptools:

[metadata]
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

[options]
zip_safe = False
include_package_data = True
packages = find:
scripts =
    bin/first.py
    bin/second.py
install_requires =
    requests
    importlib; python_version == "2.6"

[options.package_data]
* = *.txt, *.rst
hello = *.msg

[options.entry_points]
console_scripts =
    executable-name = package.module:function

[options.extras_require]
pdf = ReportLab>=1.2; RXP
rest = docutils>=0.3; pack ==1.1, ==1.3

[options.packages.find]
exclude =
    src.subpackage1
    src.subpackage2

Equivalent to textbook-level writing

Copy our metafile in

https://setuptools.pypa.io/en/latest/userguide/declarative_config.html

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

https://pypa-build.readthedocs.io/en/stable/index.html

Address of the build tool

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

https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments

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

https://zhuanlan.zhihu.com/p/42561895
 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

 sphinx-quickstart

Perform this

Press as prompted

structure

And it's built

https://docs.readthedocs.io/en/latest/intro/import-guide.html

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