• poetry(python依赖管理和打包工具)快速入门 && 项目实例


    官网:https://python-poetry.org/docs/

    PoetryPython 中用于依赖管理和打包的工具。它允许您声明项目所依赖的库,并将为您管理(安装/更新)它们。

    安装&更新&卸载

    安装

    **不推荐使用pip install --user poetry**安装

    安装前需先安装Python环境

    以下命令中管道符后需替换到指定python版本

    • OSX/Linux系统 - 直接在终端安装
    curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
    
    • 1
    • Windows系统 - 建议在powershell安装
    (Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -
    
    • 1

    安装完后,poetry会在bin下自动生成可执行程序。

    • OSX/Linux系统 - 在$HOME/.poetry/bin
    • Windows系统 - 在%USERPROFILE%\.poetry\bin

    校验是否安装成功:

    ❯ poetry --version
    Poetry version 1.1.14

    更新

    poetry self update
    poetry self update --preview  # 安装的是 `pre-release`版本
    poetry self update 0.8.0  # 更新到指定版本
    
    • 1
    • 2
    • 3

    卸载

    curl -sSL https://install.python-poetry.org | python3 - --uninstall
    curl -sSL https://install.python-poetry.org | POETRY_UNINSTALL=1 python3 -
    
    • 1
    • 2

    基本使用

    初始化配置

    以下以已存在的仓库项目为例

    • 创建或拉取一个现有仓库的项目
    mkdir ibn-quality-be && cd ibn-quality-be
    
    • 1
    • 生成poetry配置
    poetry init
    # 根据提示完善信息,或一路回车后在`pyproject.toml`文件中维护
    
    • 1
    • 2
    • 配置poetry安装源
    [[tool.poetry.source]]
    name = 'aliyun.mirrors'
    url = "http://mirrors.aliyun.com/pypi/simple/"
    
    • 1
    • 2
    • 3

    更改配置

    • 查看配置 - poetry config --list

    • 重点关注

      • cache-dir - 缓存目录,默认即可
      • virtualenvs.create = true - 默认为true。当执行poetry install或poetry add命令时,若当前没有虚拟环境则会自动创建;为false时会自动安装到系统环境
      • virtualenvs.in-project = null - 默认为null。设置为true时,会在当前项目目录下创建虚拟环境
      • virtualenv.path - 虚拟环境的存储路径
    • 更改配置 - poetry config 配置名 配置值

      # 默认习惯,将虚拟环境创建在项目目录下,可统一管理
      poetry config virtualenvs.in-project true
      
      • 1
      • 2

    常用命令

    命令含义备注
    poetry new xxx创建新的python项目一般建议在已存在的工程下使用poetry init来初始化
    poetry init在已存在的工程下初始化会自动生成pyproject.toml文件
    poetry install xxx根据pyproject.toml文件配置安装依赖
    poetry update更新依赖
    poetry add xxx添加xxx进pyproject.toml文件,同时安装依赖若一次安装多个依赖,则用空格分隔,如:poetry add a b c
    支持指定版本安装,可见:https://python-poetry.org/docs/master/cli/#add
    poetry remove xxx删除依赖
    poetry show xxx查看可用的包信息
    poetry build构建并归档
    poetry publish发布到远程存储库
    poetry config --list查看配置

    生效&失效虚拟环境

    生效虚拟环境

    • 【建议】 方法一:执行poetry shell,会自动生效当前项目的虚拟环境
    # 会自动调用虚拟环境下的`activate`脚本生效
    ❯ poetry shell
    Spawning shell within /Users/xxx/Work/projects/xiaomi/ibn-quality/ibn-quality-be/.venv
    ❯ . /Users/xxx/Work/projects/xiaomi/ibn-quality/ibn-quality-be/.venv/bin/activate
    
    • 1
    • 2
    • 3
    • 4
    • 方法二:手动进入虚拟环境目录,执行source {path_to_venv}/bin/activate ,powershell用户执行source {path_to_venv}\Scripts\activate.ps1

    查看项目虚拟环境路径:poetry env info --path

    失效虚拟环境

    • 如果使用poetry shell,则直接执行exit即可退出虚拟环境
    • 如果手动生效虚拟环境,则执行deactivate退出虚拟环境

    🌰:poetry与django联合

    • 安装poetry - 见上文

    • 更改poetry配置:poetry config virtualenvs.in-project true

    • 创建项目目录:mkdir ibn-quality-be && cd ibn-quality-be

    • 初始化poetry:poetry init,可一路回车

    • 修改项目基础配置 - pyproject.toml

      [tool.poetry]
      name = "ibn-quality-be"
      version = "0.1.0"
      description = "xx区质量委质量后台服务"
      authors = ["xxx "]
      
      [tool.poetry.dependencies]
      python = "^3.10"
      
      [tool.poetry.dev-dependencies]
      
      [build-system]
      requires = ["poetry-core>=1.0.0"]
      build-backend = "poetry.core.masonry.api"
      
      [[tool.poetry.source]]
      name = 'aliyun.mirrors'
      url = "https://mirrors.aliyun.com/pypi/simple/"
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
    • 添加依赖,如django/djangorestframework - poetry add django djangorestframwork

    • 激活虚拟环境 - poetry shell

    • 新建django项目 - django-admin startproject ibn_quality_be .(注意最后有一个点,表示在当前目录下创建)

    • 创建apps目录 - 应用程序统一目录管理 - cd ibn-quality-be && mkdir apps

    • 创建应用程序 - cd apps && django-admin startapp message_template

      # 最终项目目录如下:/Users/xxx/Work/projects/xiaomi/ibn-quality/ibn-quality-be
      ❯ tree ibn-quality-be -I .venv
      ibn-quality-be
      ├── apps
      │   └── message_template
      │       ├── __init__.py
      │       ├── admin.py
      │       ├── apps.py
      │       ├── migrations
      │       │   └── __init__.py
      │       ├── models.py
      │       ├── tests.py
      │       └── views.py
      ├── ibn_quality_be
      │   ├── __init__.py
      │   ├── asgi.py
      │   ├── settings.py
      │   ├── urls.py
      │   └── wsgi.py
      ├── manage.py
      ├── poetry.lock
      └── pyproject.toml
      
      4 directories, 15 files
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    如何让电脑待机而wifi不关的操作方法!!
    OP-TEE中的线程管理(一)
    java面试自我介绍
    使用 VirtualBox+Vagrant 创建 CentOS7 虚拟机
    PAT 1033 To Fill or Not to Fill
    国庆day4
    TP-link TL-R473G配置pppoe账号分配网络上网
    《HCIP-openEuler实验指导手册》1.3Apache动态功能模块加载卸载练习
    助力智慧交通,开发构建道路交通场景下智能车辆检测识别系统
    强网杯2022 web
  • 原文地址:https://blog.csdn.net/Amio_/article/details/126318486