• 如何在矩池云使用 Poetry 管理项目环境


    官网介绍:Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

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

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

    本文将带大家在矩池云上安装并使用 Poetry 管理项目环境,默认你已经在矩池云上租用了一台机器,如果不知道如何在矩池云租用服务器,可以查看矩池云新手入门教程

    安装Poetry

    pip install poetry
    
    • 1

    创建一个poetry项目目录

    首先我们进入 /home 目录中,然后执行poetry new指令,即可新建一个poetry项目,默认包含下面几部分。

    cd /home
    poetry new my-project
    cd my-project
    tree
    
    • 1
    • 2
    • 3
    • 4

    my-project
    ├── README.rst   # 项目说明
    ├── my_project   # 项目文件目录
    │   └── __init__.py
    ├── pyproject.toml  # poetry配置文件 重要
    └── tests   # 测试文件
        ├── __init__.py
        └── test_my_project.py
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    上面目录结构中,最重要的是pyproject.toml,里面默认包含了下面内容:

    [tool.poetry]
    name = "my-project"
    version = "0.1.0"
    description = ""
    authors = ["Your Name "]
    
    [tool.poetry.dependencies]
    python = "^3.8"
    
    [tool.poetry.dev-dependencies]
    pytest = "^5.2"
    
    [build-system]
    requires = ["poetry-core>=1.0.0"]
    build-backend = "poetry.core.masonry.api"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • tool.poetry 记录项目名称、版本、基本描述和作者
    • tool.poetry.dependencies 记录项目依赖工具和版本,比如python
    • tool.poetry.dev-dependencies 记录项目依赖的python包
    • build-system 记录Poetry环境构建工具

    poetry创建、进入虚拟环境

    • 创建虚拟环境 poetry env use 本地python解释器路径
    (myconda) root@c6854bdc088b:/home/my-project# poetry env use /root/miniconda3/envs/myconda/bin/python
    Creating virtualenv my-project-zjY4rh4o-py3.8 in /root/.cache/pypoetry/virtualenvs
    Using virtualenv: /root/.cache/pypoetry/virtualenvs/my-project-zjY4rh4o-py3.8
    
    • 1
    • 2
    • 3
    • 查看虚拟环境基本信息
    (myconda) root@c6854bdc088b:/home/my-project# poetry env info
    
    Virtualenv
    Python:         3.8.2
    Implementation: CPython
    Path:           /root/.cache/pypoetry/virtualenvs/my-project-zjY4rh4o-py3.8
    Valid:          True
    
    System
    Platform: linux
    OS:       posix
    Python:   /root/miniconda3/envs/myconda
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 进入虚拟环境
    (myconda) root@c6854bdc088b:/home/my-project# poetry shell
    Spawning shell within /root/.cache/pypoetry/virtualenvs/my-project-zjY4rh4o-py3.8
    sh-4.4# . /root/.cache/pypoetry/virtualenvs/my-project-zjY4rh4o-py3.8/bin/activate
    (my-project-zjY4rh4o-py3.8) sh-4.4# pip list
    Package    Version
    ---------- -------
    pip        22.0.4
    setuptools 62.1.0
    wheel      0.37.1
    (my-project-zjY4rh4o-py3.8) sh-4.4# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    poetry常用指令

    • 安装第三方包

    进入虚拟环境后,我们可以直接pip innstall 包名 安装自己需要的第三方包,不过这样安装包不会记录到pyproject.toml中。

    # poetry shell 进入虚拟环境后,可以直接pip install 包名安装
    (my-project-zjY4rh4o-py3.8) sh-4.4# pip install pandas
    Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
    Collecting pandas
      Downloading https://mirrors.aliyun.com/pypi/packages/12/07/e82b5de
    ...
    Successfully installed numpy-1.22.3 pandas-1.4.2 python-dateutil-2.8.2 pytz-2022.1 six-1.16.0
    (my-project-zjY4rh4o-py3.8) sh-4.4# pip list
    Package         Version
    --------------- -------
    numpy           1.22.3
    pandas          1.4.2
    pip             22.0.4
    python-dateutil 2.8.2
    pytz            2022.1
    setuptools      62.1.0
    six             1.16.0
    wheel           0.37.1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    不进入虚拟环境,我们可以通过poetry add 包名来安装,同时会生成一个poetry.lock文件,记录安装包相关依赖。

    # 不进入虚拟环境
    (myconda) root@cd90f1a3f442:/home/my-project# poetry add pendulum@latest
    Using version ^2.1.2 for pendulum
    
    Updating dependencies
    Resolving dependencies... (59.4s)
    
    Writing lock file
    
    Package operations: 10 installs, 0 updates, 0 removals
    
      • Installing pyparsing (3.0.8)
      • Installing attrs (21.4.0)
      • Installing more-itertools (8.12.0)
      • Installing packaging (21.3)
      • Installing pluggy (0.13.1)
      • Installing py (1.11.0)
      • Installing pytzdata (2020.1)
      • Installing wcwidth (0.2.5)
      • Installing pendulum (2.1.2)
      • Installing pytest (5.4.3)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 查看安装的包依赖关系 poetry show -t
    (myconda) root@cd90f1a3f442:/home/my-project# poetry show -t
    pendulum 2.1.2 Python datetimes made easy
    ├── python-dateutil >=2.6,<3.0
    │   └── six >=1.5 
    └── pytzdata >=2020.1
    pytest 5.4.3 pytest: simple powerful testing with Python
    ├── atomicwrites >=1.0
    ├── attrs >=17.4.0
    ├── colorama *
    ├── more-itertools >=4.0.0
    ├── packaging *
    │   └── pyparsing >=2.0.2,<3.0.5 || >3.0.5 
    ├── pluggy >=0.12,<1.0
    ├── py >=1.5.0
    └── wcwidth *
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 移除安装的第三方包 poetry remove 包名,移除指定第三方包的同时会卸载相关依赖包。
    (myconda) root@cd90f1a3f442:/home/my-project# poetry remove pendulum
    Updating dependencies
    Resolving dependencies... (0.1s)
    
    Writing lock file
    
    Package operations: 0 installs, 0 updates, 4 removals
    
      • Removing pendulum (2.1.2)
      • Removing python-dateutil (2.8.2)
      • Removing pytzdata (2020.1)
      • Removing six (1.16.0)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 导出项目依赖
    poetry export -f requirements.txt --output requirements.txt
    
    • 1

    常用参数:

    --format (-f): 导出文件格式,目前仅支持requirements.txt
    --output (-o): 导出文件名称
    
    • 1
    • 2
    • 查看poetry全局配置 poetry config --list
    (myconda) root@cd90f1a3f442:/home/my-project# poetry config --list
    # poetry缓存目录
    cache-dir = "/root/.cache/pypoetry"
    experimental.new-installer = true
    installer.parallel = true
    # 默认 true,进行poetry add/install 时如果没有虚拟环境,就创建一个,如果为 false,没有虚拟环境就安装到系统环境中
    virtualenvs.create = true
    # 在项目根目录创建虚拟环境
    virtualenvs.in-project = null
    # 虚拟环境目录
    virtualenvs.path = "{cache-dir}/virtualenvs"  # /root/.cache/pypoetry/virtualenvs
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 设置poetry全局配置值 poetry config virtualenvs.create false --local
    (myconda) root@cd90f1a3f442:/home/my-project# poetry config virtualenvs.create false --local
    (myconda) root@cd90f1a3f442:/home/my-project# poetry config --list
    cache-dir = "/root/.cache/pypoetry"
    experimental.new-installer = true
    installer.parallel = true
    virtualenvs.create = false
    virtualenvs.in-project = null
    virtualenvs.path = "{cache-dir}/virtualenvs"  # /root/.cache/pypoetry/virtualenvs
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    执行后,会在项目目录下生成一个poetry.toml文件,记录了修改的配置名和对应的值,–local表示这是修改本项目的配置。

    更多相关指令
    poetry install  # 通过项目目录中的pyproject.toml安装相关依赖
    poetry check  # 检查依赖关系
    poetry search requests  # 查找可用的相关包信息
    poetry lock # 更新 pyproject.toml 中依赖版本,加--no-update只更lock新文件,不更新包版本
    poetry version  # 查看poetry版本
    ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    更多使用方法,可以阅读学习官方文档:https://python-poetry.org/docs/cli

  • 相关阅读:
    解锁 DevOps 精通:成功的综合指南
    1000套web前端期末大作业 HTML+CSS+JavaScript网页设计实例 企业网站制作【建议收藏】
    Django如何快速连接MQTT
    React之配置多个代理实现数据请求返回
    移动端统计分析工具Firebase、AppsFlyer、Adjust、Flurry、Tap stream、Kochava 、branch不完全对比分析
    Java 大文件分片上传
    linux下netlink的使用
    蔚来杯_2022牛客暑期多校训练营(加赛) E.Everyone is bot
    好心情平台:30分钟就可改善抑郁情绪的运动处方
    stft的窗函数设计要求和方法(COLA)
  • 原文地址:https://blog.csdn.net/weixin_48344945/article/details/127806887