Python Publish Package

Reference: How to publish open-source python package to pypi[1]

PEPs about Python Packaging

The most important documents that define how Python packaging works are the following PEPs:

  • PEP 427 describes how wheels should be packaged.
  • PEP 440 describes how version numbers should be parsed.
  • PEP 508 describes how dependencies should be specified.
  • PEP 517 describes how a build backend should work.
  • PEP 518 describes how a build system should be specified.
  • PEP 621 describes how project metadata should be written.
  • PEP 660 describes how editable installs should be performed.

Project Structure

In the reference article[1], it shows a demo project:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
realpython-reader/

├── src/
│ └── reader/
│ ├── __init__.py
│ ├── __main__.py
│ ├── config.toml
│ ├── feed.py
│ └── viewer.py

├── tests/
│ ├── test_feed.py
│ └── test_viewer.py

├── LICENSE
├── MANIFEST.in
├── README.md
└── pyproject.toml

And I will explain their function one by one: - src: hold all the codes - reader: the core module - __init__.py: represents the root of your package, store some constant into here - __main__.py: Indeed, when executing a package with python -m as you did earlier, Python runs the contents of __main__.py.In other words, __main__.py acts as the entry point of your program and takes care of the main flow. - config.toml: config.toml is a configuration file used to specify the URL of the feed of Real Python tutorials. - *.py: some otehr codes - tests: create tests by pytest - pyproject.toml: the profile of the project

The detailed info are shown on the reference site, and here I would like to show an easier example. And here is my project struct:

1
2
3
4
5
6
7
8
9
10
.
├── README.md
├── lib
├── pyproject.toml
├── scripts
│   └── obs2hexo.sh
└── src
└── o2h
├── __init__.py
└── translator.py

And here is my pyproject.toml file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[build-system]
requires = ["setuptools>=61.0.0", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "o2h"
version = "1.2.0"
description = "Publish obsidian to hexo github page"
readme = "README.md"
authors = [{ name = "Chivier Humber"}]
license = { file = "LICENSE" }
classifiers = [
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
]
keywords = ["feed", "reader", "tutorial"]
dependencies = [
"feedparser >= 5.2.0",
"html2text",
]
requires-python = ">=3.9"

[project.optional-dependencies]
dev = ["black", "bumpver", "isort", "pip-tools", "pytest"]

[project.urls]
Homepage = "https://github.com/Chivier/H2O2H"

[project.scripts]
obs2hexo = "o2h.translator:o2h"

The last section should be emphasized, there are 2 o2h in the last line:

The first o2h is the name of the module, and the second one is the name of function.

Eventually, I build a connection between the command obs2hexo and the function o2h.


Python Publish Package
http://blog.chivier.site/2023-02-05/2023/Python-Publish-Package/
Author
Chivier Humber
Posted on
February 5, 2023
Licensed under