Python包管理工具区分
- pip: 包管理工具
- vitualenv:虚拟环境管理工具
- pyenv:python版本管理工具(支持下载指定python版本)
- pipenv和poetry:项目环境管理工具
- pipx:python应用管理工具,在隔离环境中安装和运行python应用
- venv:虚拟环境管理工具(python3.3 以后自带)
通过Toml文件定义环境依赖
pyproject.toml
[tool.poetry]
name = "example"
version = "0.1.0"
description = "A example project"
authors = ["aiden <aiden_lu@wochacha.com>"]
[[tool.poetry.source]]
name = "wcc-pypi"
url = "https://pypi.wochacha.cn/simple/"
default = false
[[tool.poetry.source]]
name = "default"
url = "https://pypi.org/simple/"
default = false
[[tool.poetry.source]]
name = "aliyun-pypi"
url = "https://mirrors.aliyun.com/pypi/simple/"
default = true
[tool.poetry.dependencies]
python = "^3.9"
pytz = "^2021.3" # https://github.com/stub42/pytz
python-slugify = "^5.0.2" # https://github.com/un33k/python-slugify
Pillow = "^9.0.0" # https://github.com/python-pillow/Pillow
argon2-cffi = "^21.3.0" # https://github.com/hynek/argon2_cffi
redis = "^4.1.0" # https://github.com/redis/redis-py
hiredis = "^2.0.0" # https://github.com/redis/hiredis-py
celery = "^5.2.3" # pyup: < 6.0 # https://github.com/celery/celery
django-celery-beat = "^2.2.1" # https://github.com/celery/django-celery-beat
uvicorn = { version = "^0.16.0", extras = ["standard"], optional = true } # https://github.com/encode/uvicorn
psycopg2 = { version = "^2.9.3", optional = true } # https://github.com/psycopg/psycopg2
sentry-sdk = { version = "^1.5.1", optional = true } # https://github.com/getsentry/sentry-python
gunicorn = { version = "^20.1.0", optional = true } # https://github.com/benoitc/gunicorn
# Django
# ------------------------------------------------------------------------------
django = "^3.2.10" # pyup: < 4.0 # https://www.djangoproject.com/
django-environ = "^0.8.1" # https://github.com/joke2k/django-environ
django-model-utils = "^4.2.0" # https://github.com/jazzband/django-model-utils
django-allauth = "^0.47.0" # https://github.com/pennersr/django-allauth
django-crispy-forms = "^1.13.0" # https://github.com/django-crispy-forms/django-crispy-forms
crispy-bootstrap5 = "^0.6" # https://github.com/django-crispy-forms/crispy-bootstrap5
django-redis = "^5.2.0" # https://github.com/jazzband/django-redis
channels = "^3.0.4"
django-storages = { version = "^1.12.3", extras = ["boto3"], optional = true } # https://github.com/jschneier/django-storages
django-anymail = { version = "^8.4", optional = true } # https://github.com/anymail/django-anymail
# Django REST Framework
djangorestframework = "^3.13.1" # https://github.com/encode/django-rest-framework
django-cors-headers = "^3.10.1" # https://github.com/adamchainz/django-cors-headers
authing = "^4.5.16" # https://github.com/Authing/authing-py-sdk
pypinyin = "^0.45.0"
itsdangerous = "^2.0.1"
drfexts = { version = "^0.4.2", source = "default" }
django-user-notification = { version = "^0.4.0", source = "default" }
djangorestframework-simplejwt = "^5.0.0"
drf-spectacular = "^0.21.1"
ujson = "^5.1.0"
drf-flex-fields = "^0.9.7"
channels-redis = "^3.3.1"
[tool.poetry.dev-dependencies]
Werkzeug = "^2.0.2" # https://github.com/pallets/werkzeug
ipdb = "^0.13.9" # https://github.com/gotcha/ipdb
psycopg2-binary = "^2.9.3" # https://github.com/psycopg/psycopg2
watchgod = "^0.7" # https://github.com/samuelcolvin/watchgod
# Testing
# ------------------------------------------------------------------------------
mypy = "^0.930" # https://github.com/python/mypy
django-stubs = "^1.9.0" # https://github.com/typeddjango/django-stubs
pytest = "^6.2.5" # https://github.com/pytest-dev/pytest
pytest-sugar = "^0.9.4" # https://github.com/Frozenball/pytest-sugar
djangorestframework-stubs = "^1.4.0" # https://github.com/typeddjango/djangorestframework-stubs
# Documentation
# ------------------------------------------------------------------------------
sphinx = "^4.3.2" # https://github.com/sphinx-doc/sphinx
sphinx-autobuild = "^2021.3.14" # https://github.com/GaretJax/sphinx-autobuild
# Code quality
# ------------------------------------------------------------------------------
flake8 = "^4.0.1" # https://github.com/PyCQA/flake8
flake8-isort = "^4.1.1" # https://github.com/gforcada/flake8-isort
coverage = "^6.2" # https://github.com/nedbat/coveragepy
black = "^21.12b0" # https://github.com/psf/black
pylint-django = "^2.5.0" # https://github.com/PyCQA/pylint-django
pylint-celery = "^0.3" # https://github.com/PyCQA/pylint-celery
pre-commit = "^2.16.0" # https://github.com/pre-commit/pre-commit
# Django
# ------------------------------------------------------------------------------
factory-boy = "^3.2.1" # https://github.com/FactoryBoy/factory_boy
django-debug-toolbar = "^3.2.4" # https://github.com/jazzband/django-debug-toolbar
django-extensions = "^3.1.5" # https://github.com/django-extensions/django-extensions
django-coverage-plugin = "^2.0.2" # https://github.com/nedbat/django_coverage_plugin
pytest-django = "^4.5.2" # https://github.com/pytest-dev/pytest-django
# Django REST Framework
# ------------------------------------------------------------------------------
drf-spectacular = "^0.21.1" # https://github.com/tfranzel/drf-spectacular
[tool.poetry.extras]
production = ["uvicorn", "psycopg2", "sentry-sdk", "gunicorn", "django-storages", "django-anymail"]
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Poetry简单命令介绍
- 创建pyproject.toml文件:poetry init
- 安装依赖:poetry install
- 新增一个包: poetry add package_name
新增一个包(dev环境): poetry add package_name —dev
移除一个包:poetry remove package_name
- 更新包版本:poetry update
安装完poetry之后 执行下面命令 这样虚拟环境就会在项目根目录底下创建
poetry config virtualenvs.in-project true
poetry env use c:/python311/python.exe
DRF 开发环境搭建
方式一: 本地开发环境搭建(poetry 安装、pycharm poetry集成及添加、删除、更新一个包)
- 安装poetry
方式二: 使用docker搭建开发环境
使用pycharm来debug以及使用命令行调试(shell)
pycharm提供了很好的debug工具
使用django 命令行调试工具: python manage.py shell
使用black、isort、flake8 来提高代码质量
!pre-commit的使用(强烈建议使用)
1.安装pre-commit包
pip install pre-commit
# 在指定项目下生效
pre-commit install
2.pre-commit配置文件
# 不效验, 直接push
git commit -m "XXX" --no-verify
git push
“.pre-commit-config.yaml”
exclude: "^docs/|/migrations/"
default_stages: [commit]
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-toml
- id: check-merge-conflict
- id: debug-statements
- repo: https://github.com/psf/black
rev: 21.12b0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
args: ["--profile", "black"]
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
args: ["--config=setup.cfg"]
additional_dependencies: [flake8-isort]
# sets up .pre-commit-ci.yaml to ensure pre-commit dependencies stay up to date
ci:
autoupdate_schedule: weekly
skip: []
submodules: false
yaml
使用gitlab ci来进行单元测试
- 项目根目录定义一个”.gitlab-ci.yml” 文件
- 设置触发条件(branch名称or tag名称)
- 编写单元测试
使用cookiecutter来初始化一个项目
安装cookiecutter
pip install “cookiecutter>=1.7.0”
根据模版生成项目
cookiecutter https://gl.xxx.cn/wcc-framework/wcc-backend-cookiecutter.git
0则评论给“python开发环境的搭建”