虚拟环境工具
安装poetry
pip install -U peotry
上述方式不行时
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python
加入环境变量
初始化项目
安装cookiecutter
pip install -U cookiecutter
初始化项目
cd workspace
cookiecutter https://github.com/pyloong/cookiecutter-pythonic-project
D:\project\py_pro_guide>cookiecutter https://github.com/pyloong/cookiecutter-pythonic-project[1/12] project_name (My Project): Time Count [2/12] project_slug (time_count):[3/12] project_description (My Awesome Project!): to count time used[4/12] author_name (Author): qrrk[5/12] author_email (qrrk@example.com): qierenrongku@qq.com[6/12] version (0.1.0):[7/12] Select python_version1 - 3.102 - 3.11Choose from [1/2] (1): 2[8/12] use_src_layout (y):[9/12] use_poetry (y):[10/12] use_docker (n):[11/12] Select ci_tools1 - none2 - Gitlab3 - GithubChoose from [1/2/3] (1):[12/12] init_skeleton (n):
会生成如下的工程目录
D:\project\py_pro_guide>tree /f
D:.
└─time_count│ .editorconfig│ .gitignore│ .pre-commit-config.yaml│ LICENSE│ pyproject.toml # 目初始依赖,和项目的描述信息│ README.md│ tox.ini # 任务自动化执行逻辑│├─docs│ development.md│├─src # 放源代码│ └─time_count│ __init__.py│└─tests # 测试目录conftest.pysettings.ymltest_version.py__init__.py
初始化项目环境
使用 poetry 初始化一个虚拟环境
D:\project\py_pro_guide>cd time_count
D:\project\py_pro_guide\time_count>poetry install -v
**C:\Users\xsc\AppData\Local\pypoetry\Cache\virtualenvs\time-count-dAro35JJ-py3.11\Scripts**
一些工具
在生成的 pyproject.toml
文件中,默认添加了一些开发环境中常用的工具。
isort
: isort 是一个自动格式化导入工具pylint
: pylint 是一个检测代码风格工具pytest
: pytest 是一个更加易用的测试框架,兼容unittest
测试框架pytest-cov
: pytest-cov 是pytest
的 Coverage 插件,用来统计测试覆盖率mkdocs
: mkdocs 是一个项目文档构建工具,使用 markdown 编写内容,构建生成文档页面。mkdocs-material
: mkdocs-material 是基于 mkdocs 构建文档,并提供现代化主题的库。tox
: tox 是一个任务自动化工具
项目开发
先将项目安装到python环境中
D:\project\py_pro_guide\time_count>poetry install
Installing dependencies from lock fileNo dependencies to install or updateInstalling the current project: time_count (0.1.0)
编写代码
isort . # 在项目根目录运行 isort对导入进行格式化
isort . --check-only --diff # 此操作会自动修改代码,将导入的包格式化
pylint tests src # 项目根目录运行pylint检查代码是否规范
测试
poetry add --group dev pytest-mock
如果感觉每次运行多个命令比较繁琐,可以在项目根目录中运行 tox
自动化完成代码测试、导包检查和代码风格检查。
tox
打包发布
如果希望别人能更方便的使用项目,可以将项目打包发布到 pypi 中,然后在需要使用的地方运行
pip install -U word-count
但是安装到环境后去运行 cmdline.py
会比较麻烦,所以需要将 cmdline.py
注册成可执行命令。
修改 pyproject.toml
,增加如下内容:
[tool.poetry.plugins.console_scripts]
time_count = "time_count.cmdline:main"`
当使用 pip
命令将项目包安装到环境后,会自动注册一个 time_count
的可执行命令。
再次将本地项目以可编辑方式安装到当前 Python 环境:
poetry install`
然后就可以正常使用 time_count` 命令:
$ time_count -h
usage: word_count [-h] -s SOURCE -d DESToptional arguments:
-h, --help show this help message and exit -s SOURCE,
--source SOURCE Source file used for count.
-d DEST,
--dest DEST Dest file used for count result
运行打包命令:
poetry build`
sdist
会将项目打包成源码包, bdist_wheel
会将项目打包成编译后的二进制包。
打包后的文件在 dist
目录中。可以直接在其他地方运行 pip install word_count.wheel
安装。
发布
将开发好的项目发布到索引仓库,或内网的私有仓库。
poetry publish`
默认会将项目发布到 pypi 中,所以需要有对应的登录账号。
参考:快速上手 - Python 项目工程化开发指南 (pyloong.github.io)