myteamtest
一个可通过 pip install 安装的 Python 工具包示例工程。
采用现代 pyproject.toml + src 布局,开箱即用,可直接改名后作为你自己的包发布到 PyPI。
目录结构
myteamtest/
├── pyproject.toml # 唯一的构建/元数据配置文件(替代 setup.py + setup.cfg)
├── README.md
├── LICENSE
├── .gitignore
├── src/
│ └── myteamtest/
│ ├── __init__.py # 对外导出的 API
│ ├── core.py # 核心功能
│ ├── utils.py # 辅助工具
│ ├── cli.py # 命令行入口
│ └── py.typed # 标记该包带类型提示
├── tests/
│ ├── test_core.py
│ ├── test_utils.py
│ └── test_cli.py
└── .github/workflows/ci.yml
使用
src/布局的好处:导入时用的是安装后的包而不是当前目录的源码, 能提前暴露"漏声明的子包""忘记加依赖"这类只在用户机器上才会炸的问题。
快速开始
# 1. 创建虚拟环境(推荐)
python -m venv .venv
.venv\Scripts\activate # Windows
# source .venv/bin/activate # Linux / macOS
# 2. 可编辑安装(改代码立即生效,适合开发)
pip install -e ".[dev]"
# 3. 跑测试
pytest
# 4. 试用命令行(由 [project.scripts] 生成)
myteamtest --help
myteamtest count README.md -n 5
myteamtest size 1048576
# 也支持管道
echo "hello hello world" | myteamtest count
作为库使用
from myteamtest import word_count, human_readable_size, Timer, chunked
word_count("Hello hello world", top_n=2) # {'hello': 2, 'world': 1}
human_readable_size(1536) # '1.50 KB'
with Timer() as t:
...
print(t.elapsed)
打包与安装
pip install build twine
# 构建(生成 dist/myteamtest-0.1.0-py3-none-any.whl 和 .tar.gz)
python -m build
# 本地验证 wheel 能装能用
pip install dist/myteamtest-0.1.0-py3-none-any.whl
python -c "import myteamtest; print(myteamtest.__version__)"
发布到 PyPI
# 1. 先发到测试环境演练
twine upload --repository testpypi dist/*
pip install --index-url https://test.pypi.org/simple/ myteamtest
# 2. 正式发布(需要 PyPI Token)
twine upload dist/*
Windows 一键发布
$env:TWINE_USERNAME = "__token__"
$env:TWINE_PASSWORD = "pypi-你的API令牌"
.\publish.ps1 # 正式发布
.\publish.ps1 -TestPyPI # 先演练到 TestPyPI
发布前检查清单:
-
pyproject.toml里version已递增 -
name在 PyPI 上未被占用 -
python -m build与twine check dist/*均通过 -
pytest全绿
改成你自己的包
- 把目录
src/myteamtest/重命名为src/<你的包名>/ - 全局替换
myteamtest→<你的包名>(pyproject.toml、cli.py、tests/、本文件) - 修改
pyproject.toml中的authors、urls、description - 重新
pip install -e .
常用命令备忘
| 目的 | 命令 |
|---|---|
| 开发安装 | pip install -e ".[dev]" |
| 运行测试 | pytest |
| 覆盖率 | pytest --cov=myteamtest --cov-report=term-missing |
| 代码检查 | ruff check src tests |
| 构建分发包 | python -m build |
| 上传 PyPI | twine upload dist/* |
许可证
MIT
Release files for myteamtest 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| myteamtest-0.1.0.tar.gz | 8.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| myteamtest-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.7 kB
Release files / myteamtest-0.1.0.tar.gz
| Download URL | myteamtest-0.1.0.tar.gz |
|---|---|
| Size | 8.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0e9210c6e292c2570078b9f34eb3a9bfda531937dba33df8bdd01e1adbd9d815
|
|
BLAKE2b-256 checksum How to use checksums |
9a4d3789c4336bc00289dfd7c145c11ab079c7b5ff7c76b8d8dc2dfd40b65b7d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.5
|
Release files / myteamtest-0.1.0-py3-none-any.whl
| Download URL | myteamtest-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
29bcd96d31ca7aa8439c3e44f6598e72cd4248e4e6836f901aea86d6603bbfa4
|
|
BLAKE2b-256 checksum How to use checksums |
7ff8cf679661d472a6616845a2119531d9d6611fa5394f0771ce2c7a5efe40b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.5
|