Skip to content

Commit 56e7977

Browse files
author
zhenwei-li
committed
构建与分发
1 parent 985d738 commit 56e7977

1 file changed

Lines changed: 328 additions & 2 deletions

File tree

README.md

Lines changed: 328 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,22 @@
2828
- [4.3 virtualenv version](#43-virtualenv-version)
2929
- [4.4 tox tox-travis version](#44-tox-tox-travis-version)
3030
- [4.5 twine version](#45-twine-version)
31-
- [五. 参考](#五-参考)
31+
- [五. 构建与分发](#五-构建与分发)
32+
- [5.1 安装](#51-安装)
33+
- [5.2 配置](#52-配置)
34+
- [5.2.1 pyproject.toml](#521-pyprojecttoml)
35+
- [5.2.2 MANIFEST.in](#522-manifestin)
36+
- [5.2.3 tox.ini](#523-toxini)
37+
- [5.2.4 setup.cfg](#524-setupcfg)
38+
- [5.2.5 README.md](#525-readmemd)
39+
- [5.3 构建](#53-构建)
40+
- [5.3.1 tox 脚本测试](#531-tox-脚本测试)
41+
- [5.3.2 build](#532-build)
42+
- [5.3 发布](#53-发布)
43+
- [5.3.1 注册账号](#531-注册账号)
44+
- [5.3.2 检查软件包](#532-检查软件包)
45+
- [5.3.3 上传软件包](#533-上传软件包)
46+
- [六. 参考](#六-参考)
3247

3348
## 一. 构建`venv`环境
3449

@@ -274,7 +289,318 @@ twine check dist/*
274289
twine upload dist/*
275290
```
276291

277-
## 五. 参考
292+
## 五. 构建与分发
293+
294+
Python 软件包开发工程结构,如下所示:
295+
296+
```Django
297+
# soft package tree
298+
299+
--- project root
300+
|
301+
| --- .tox
302+
| --- .vscode
303+
| --- build
304+
| --- dist
305+
| --- doc
306+
--- ...
307+
--- README.md
308+
| --- src
309+
--- com
310+
--- dvsnier
311+
--- ...
312+
| --- test
313+
--- com
314+
--- dvsnier
315+
--- ...
316+
| --- venv
317+
| --- venv2
318+
| --- .editorconfig
319+
| --- .gitignore
320+
| --- LICENSE.txt
321+
| --- MANIFEST.in
322+
| --- pyproject.toml
323+
| --- README.md
324+
| --- setup.cfg
325+
| --- tox.ini
326+
```
327+
328+
### 5.1 安装
329+
330+
首先检查是否安装如下依赖:
331+
332+
1. pip
333+
2. flake8 (可选)
334+
3. virtualenv
335+
4. setuptools
336+
5. wheel
337+
6. discover
338+
7. tox
339+
8. toml (可选)
340+
9. tox-travis (可选)
341+
10. build
342+
11. twine
343+
344+
如若没有,请使用`pip` 命令安装如下软件包:
345+
346+
```bash
347+
# python2 pip version
348+
python2 -m pip --version
349+
350+
python2 -m pip install flake8
351+
python2 -m pip install virtualenv
352+
python2 -m pip install setuptools
353+
python2 -m pip install wheel
354+
python2 -m pip install discover
355+
python2 -m pip install tox
356+
python2 -m pip install toml
357+
python2 -m pip install tox-travis
358+
python2 -m pip install build
359+
python2 -m pip install twine
360+
361+
# python3 pip version
362+
python3 -m pip --version
363+
364+
python3 -m pip install flake8
365+
python3 -m pip install virtualenv
366+
python3 -m pip install setuptools
367+
python3 -m pip install wheel
368+
python3 -m pip install discover
369+
python3 -m pip install tox
370+
python3 -m pip install toml
371+
python3 -m pip install tox-travis
372+
python3 -m pip install build
373+
python3 -m pip install twine
374+
```
375+
376+
其实一般安装有 `pip` 软件包的,一般都会有`setuptools``wheel` 软件包附带安装;
377+
378+
### 5.2 配置
379+
380+
#### 5.2.1 pyproject.toml
381+
382+
首先配置 `pyproject.toml` ,模板固定, 一般不需大范围改动;
383+
384+
```bash
385+
[build-system]
386+
# https://setuptools.readthedocs.io/en/latest/build_meta.html
387+
# These are the assumed default build requirements from pip:
388+
# https://pip.pypa.io/en/stable/cli/pip/#pep-517-and-518-support
389+
#
390+
# As of version 10.0, pip supports projects declaring dependencies that are
391+
# required at install time using a pyproject.toml file, in the form described
392+
# in PEP 518. When building a project, pip will install the required
393+
# dependencies locally, and make them available to the build process.
394+
# Furthermore, from version 19.0 onwards, pip supports projects specifying the
395+
# build backend they use in pyproject.toml, in the form described in PEP 517.
396+
#
397+
requires = [
398+
"setuptools>=40.8.0",
399+
"wheel"
400+
]
401+
build-backend = "setuptools.build_meta"
402+
```
403+
404+
#### 5.2.2 MANIFEST.in
405+
406+
然后配置 `MANIFEST.in` 软件包应当包含哪些信息,哪些排除配置信息, 如下所示:
407+
408+
```bash
409+
# https://packaging.python.org/guides/using-manifest-in/
410+
411+
include pyproject.toml
412+
413+
# Include the README
414+
include *.md
415+
416+
# Include the license file
417+
include LICENSE.txt
418+
```
419+
420+
同上,模板一般都是固定结构,无需太范围改动;
421+
422+
#### 5.2.3 tox.ini
423+
424+
再然后配置 `tox` 脚本自动化测试, 指定Python 虚拟环境版本, 配置信息如下:
425+
426+
```bash
427+
# tox (https://tox.readthedocs.io/) is a tool for running tests
428+
# in multiple virtualenvs. This configuration file will run the
429+
# test suite on all supported python versions. To use it, "pip install tox"
430+
# and then run "tox" from this directory.
431+
# For information see https://tox.readthedocs.io/en/latest/examples.html
432+
433+
[tox]
434+
envlist = py27, py39
435+
436+
minversion = 3.23.1
437+
438+
# Activate isolated build environment. tox will use a virtual environment
439+
# to build a source distribution from the source tree. For build tools and
440+
# arguments use the pyproject.toml file as specified in PEP-517 and PEP-518.
441+
isolated_build = true
442+
443+
# install testing framework
444+
# ... or install anything else you might need here
445+
[testenv]
446+
platform = linux: linux
447+
macos: darwin
448+
windows: win32
449+
; alwayscopy = True
450+
allowlist_externals =
451+
/bin/bash
452+
changedir =
453+
tests
454+
deps =
455+
build
456+
discover
457+
twine
458+
commands =
459+
discover
460+
; python -m unittest discover
461+
```
462+
463+
#### 5.2.4 setup.cfg
464+
465+
最后配置 `setup.cfg` 构建脚本, 指定软件包所要构建的软件细节部分, 举例配置信息如下:
466+
467+
```bash
468+
[metadata]
469+
# 1. https://setuptools.readthedocs.io/en/latest/
470+
# 2. https://setuptools.readthedocs.io/en/latest/userguide/declarative_config.html
471+
# 3. https://setuptools.readthedocs.io/en/latest/references/keywords.html
472+
name = com.dvsnier.directory
473+
version = 0.0.1.dev1
474+
author = dvsnier
475+
author_email = dovsnier@qq.com
476+
description = this is dvsnier directory.
477+
long_description = file: ./doc/description/directory/README.md
478+
long_description_content_type = text/markdown
479+
keywords = dir, development
480+
url = https://github.com/Alinvor/Python-DeMo
481+
project_urls =
482+
Documentation = https://packaging.python.org/tutorials/distributing-packages/
483+
Funding = https://donate.pypi.org
484+
Wiki = https://github.com/Alinvor/Python-DeMo/wiki
485+
Bug_Tracker = https://github.com/Alinvor/Python-DeMo/issues
486+
Source = https://github.com/Alinvor/Python-DeM
487+
platforms = any
488+
# classifiers
489+
# Development Status :: 3 - Alpha
490+
# Development Status :: 4 - Beta
491+
# Development Status :: 5 - Production/Stable
492+
classifiers =
493+
Development Status :: 3 - Alpha
494+
Programming Language :: Python :: 2.7
495+
Programming Language :: Python :: 3.8
496+
Programming Language :: Python :: 3.9
497+
License :: OSI Approved :: MIT License
498+
Operating System :: OS Independent
499+
500+
python_requires =
501+
>=2.7
502+
<4
503+
504+
# This includes the license file(s) in the wheel.
505+
# https://wheel.readthedocs.io/en/stable/user_guide.html#including-license-files-in-the-generated-wheel-file
506+
# https://choosealicense.com/
507+
license = MIT License
508+
license_files =
509+
LICENSE.txt
510+
511+
[options]
512+
package_dir =
513+
= src
514+
515+
packages = find:
516+
517+
[options.packages.find]
518+
where = src
519+
#
520+
# https://packaging.python.org/guides/distributing-packages-using-setuptools/#wheels
521+
#
522+
[bdist_wheel]
523+
universal = 1
524+
```
525+
526+
> 软件包`名称`和软件包`版本`信息, 一定要明确具体发布规则;
527+
528+
实际项目中, 可依据如上配置信息稍加修改和增添;
529+
530+
#### 5.2.5 README.md
531+
532+
更新`README.md` 版本和摘要信息;
533+
534+
### 5.3 构建
535+
536+
#### 5.3.1 tox 脚本测试
537+
538+
首次运行`tox` 命令,可使用`tox-quickstart` 命令, 依据简短提示信息进行配置:
539+
540+
```bash
541+
tox-quickstart
542+
```
543+
544+
后续运行`tox` 命令,可执行自动化脚本测试:
545+
546+
```bash
547+
tox
548+
```
549+
550+
#### 5.3.2 build
551+
552+
可使用如下命令, 进行`xxx.tar.gz` 源码包和`xxx.whl` 二进制包构建, 命令如下:
553+
554+
```bash
555+
# python build tar.gz and whl
556+
python2 -m build
557+
558+
# python build tar.gz
559+
python2 -m build --sdist
560+
# python build whl
561+
python2 -m build --wheel
562+
563+
564+
# python build tar.gz and whl
565+
python3 -m build
566+
567+
# python build tar.gz
568+
python3 -m build --sdist
569+
# python build whl
570+
python3 -m build --wheel
571+
```
572+
573+
### 5.3 发布
574+
575+
#### 5.3.1 注册账号
576+
577+
1. 账户注册地址: https://pypi.org/account/register/
578+
2. 创建一个[PyPI API令牌](https://pypi.org/help/#apitoken), 以便能够安全地上传您的项目;
579+
3. 为了避免每次上载时都必须复制和粘贴令牌,可以创建一个`$HOME/.pypirc` 文件, 参考如下:
580+
581+
```bash
582+
[pypi]
583+
username = __token__
584+
password = <the token value, including the `pypi-` prefix>
585+
```
586+
587+
#### 5.3.2 检查软件包
588+
589+
检查软件包, 命令:
590+
591+
```bash
592+
twine check dist/*
593+
```
594+
595+
#### 5.3.3 上传软件包
596+
597+
上传软件包, 命令:
598+
599+
```bash
600+
twine upload dist/*
601+
```
602+
603+
## 六. 参考
278604

279605
1. https://packaging.python.org/tutorials/packaging-projects/
280606
2. https://packaging.python.org/guides/distributing-packages-using-setuptools/

0 commit comments

Comments
 (0)