Cookiecutter-pylibrary 使用教程
1. 项目的目录结构及介绍
使用 Cookiecutter-pylibrary 生成的项目目录结构如下:
your_project/
├── .github/
│ └── workflows/
│ └── tests.yml
├── docs/
│ ├── conf.py
│ └── index.rst
├── src/
│ └── your_project/
│ ├── __init__.py
│ └── your_project.py
├── tests/
│ ├── __init__.py
│ └── test_your_project.py
├── .gitignore
├── AUTHORS.rst
├── CHANGELOG.rst
├── CONTRIBUTING.rst
├── LICENSE
├── README.rst
├── pyproject.toml
├── setup.cfg
└── tox.ini
目录结构介绍
- .github/workflows/tests.yml: GitHub Actions 的配置文件,用于自动化测试。
- docs/: 项目文档目录,包含 Sphinx 文档配置和主页。
- src/your_project/: 项目源代码目录,包含项目的主要代码文件。
- tests/: 测试代码目录,包含项目的测试文件。
- .gitignore: Git 忽略文件配置。
- AUTHORS.rst: 项目贡献者列表。
- CHANGELOG.rst: 项目变更日志。
- CONTRIBUTING.rst: 项目贡献指南。
- LICENSE: 项目许可证。
- README.rst: 项目说明文档。
- pyproject.toml: 项目构建配置文件。
- setup.cfg: 项目打包和分发配置文件。
- tox.ini: 项目测试配置文件。
2. 项目的启动文件介绍
项目的启动文件通常位于 src/your_project/
目录下,例如 your_project.py
。这个文件包含了项目的主要功能和入口点。
# src/your_project/your_project.py
def main():
print("Hello, world!")
if __name__ == "__main__":
main()
启动文件介绍
- main(): 项目的入口函数,通常包含项目的主要逻辑。
- if name == "main": 确保该脚本在直接运行时执行
main()
函数。
3. 项目的配置文件介绍
项目的配置文件主要包括 setup.cfg
、pyproject.toml
和 tox.ini
。
setup.cfg
setup.cfg
文件用于配置项目的打包和分发设置。
[metadata]
name = your_project
version = 0.1.0
description = A short description of your project
author = Your Name
author_email = your.email@example.com
license = MIT
[options]
package_dir =
= src
packages = find:
install_requires =
requests
[options.packages.find]
where = src
pyproject.toml
pyproject.toml
文件用于配置项目的构建系统和其他工具。
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
[tool.black]
line-length = 88
tox.ini
tox.ini
文件用于配置项目的自动化测试环境。
[tox]
envlist = py37,py38,py39
[testenv]
deps =
pytest
commands =
pytest
配置文件介绍
- setup.cfg: 包含项目的元数据和打包选项。
- pyproject.toml: 包含项目的构建系统和工具配置。
- tox.ini: 包含项目的测试环境配置。
通过以上配置文件,可以确保项目在不同环境和工具中的正确运行和测试。