Python 依赖管理和打包工具 Poetry 安装和测试

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

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

Poetry 安装方法有三种:

  • With the official installer
  • With pipx
  • With pip
  • 官方推荐使用官方安装脚本,位于:
    https://python-poetry.org/docs/。

    注意: get-poetry.py 脚本将在 Poetry 1.2 中被 install-poetry.py 替代,建议直接使用最新版本的安装脚本。方法是在顶部的版本选择下拉框中选择 master。

    官网默认的安装脚本仍然是get-poetry.py,在安装时过程中我注意到提示信息中含有如下提示:This installer is deprecated. Poetry versions installed using this script will not be able to use 'self update' command to upgrade to 1.2.0a1 or later.

    这才注意到选择install-poetry.py更好。

    安装

    对于Windows操作系统,需要在PowerShell下输入

    (Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -

    安装过程如下:

    注意:由于服务器位于国外,安装过程很慢,建议科学上网。

    This installer is deprecated. Poetry versions installed using this script will not be able to use 'self update' command to upgrade to 1.2.0a1 or later.
    # Welcome to Poetry!
    
    This will download and install the latest version of Poetry,
    a dependency and package manager for Python.
    
    It will add the `poetry` command to Poetry's bin directory, located at:
    
    %USERPROFILE%\.poetry\bin
    
    This path will then be added to your `PATH` environment variable by
    modifying the `HKEY_CURRENT_USER/Environment/PATH` registry key.
    
    You can uninstall at any time by executing this script with the --uninstall option,
    and these changes will be reverted.
    
    Installing version: 1.1.13
      - Downloading poetry-1.1.13-win32.tar.gz (71.14MB)
    
    Poetry (1.1.13) is installed now. Great!
    
    To get started you need Poetry's bin directory (%USERPROFILE%\.poetry\bin) in your `PATH`
    environment variable. Future applications will automatically have the
    correct environment, but you may need to restart your current shell.

    对于Linux操作系统,需要执行以下命令

    如果没有创建python符号链接,那么需要将最后的python改为python3。

    curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3 -

    安装过程如下:

    Retrieving Poetry metadata
    
    This installer is deprecated. Poetry versions installed using this script will not be able to use 'self update' command to upgrade to 1.2.0a1 or later.
    # Welcome to Poetry!
    
    This will download and install the latest version of Poetry,
    a dependency and package manager for Python.
    
    It will add the `poetry` command to Poetry's bin directory, located at:
    
    $HOME/.poetry/bin
    
    This path will then be added to your `PATH` environment variable by
    modifying the profile files located at:
    
    $HOME/.profile
    $HOME/.bash_profile
    
    You can uninstall at any time by executing this script with the --uninstall option,
    and these changes will be reverted.
    
    Installing version: 1.1.13
      - Downloading poetry-1.1.13-linux.tar.gz (97.93MB)
    
    Poetry (1.1.13) is installed now. Great!
    
    To get started you need Poetry's bin directory ($HOME/.poetry/bin) in your `PATH`
    environment variable. Next time you log in this will be done
    automatically.
    
    To configure your current shell run `source $HOME/.poetry/env`
    

    安装脚本会将poetry可执行文件安装到 Poetry 的 bin 目录中。 对于 Unix ,它位于 $HOME/.poetry/bin,对于 Windows ,它位于 %USERPROFILE%\.poetry\bin。这个目录将被自动添加到您的 $PATH 环境变量中,这是通过在 $HOME/.profile 配置文件(或等效文件)中附加一条语句完成的。例如在我的Rocky Linux中,在 .bash_profile 中可以找到这么一个语句:

    export PATH="$HOME/.poetry/bin:$PATH"

    卸载

    如果不喜欢poetry,可以直接卸载:

    python get-poetry.py --uninstall

    测试

    创建项目

    # cd test/
    # 使用poetry创建一个新项目foo
    # poetry new foo
    Created package foo in foo
    # 查看项目目录结构
    # tree
    .
    └── foo
        ├── foo
        │   └── __init__.py
        ├── pyproject.toml
        ├── README.rst
        └── tests
            ├── __init__.py
            └── test_foo.py
    
    3 directories, 5 files
    
    # 创建虚拟环境(确保当前目录有 pyproject.toml 文件)
    # cd foo
    # poetry install
    # 虚拟环境默认位于目录:/root/.cache/pypoetry/virtualenvs
    Creating virtualenv foo-eem7dH6g-py3.9 in /root/.cache/pypoetry/virtualenvs
    Updating dependencies
    Resolving dependencies... (406.4s)
    
    Writing lock file
    
    Package operations: 8 installs, 0 updates, 0 removals
    
      • Installing pyparsing (3.0.7)
      • 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 wcwidth (0.2.5)
      • Installing pytest (5.4.3)
    
    Installing the current project: foo (0.1.0)

    使用虚拟环境

    默认情况下,poetry 会在 {cache-dir}/virtualenvs(对于Windows则为 {cache-dir}\virtualenvs)中创建一个虚拟环境。 您可以通过编辑 poetry 配置来更改 {cache-dir}。 此外,您还可以使用 virtualenvs.in-project 配置变量在项目目录中创建虚拟环境。

    有几种方法可以在这个虚拟环境中运行命令。

    要运行您的脚本,只需输入 poetry run python your_script.py。 同样,如果您有 pytest 或者 black 等命令行工具,您可以在命令行输入 poetry pytest 来运行它们。

    激活虚拟环境

    激活虚拟环境最简单的方法是输入 poetry shell 创建一个新的shell。 输入 exit 停用虚拟环境并退出这个新的 shell 。 要在不离开 shell 的情况下停用虚拟环境,请使用 deactivate。

    注意:执行 poetry 开头的命令并不需要激活虚拟环境,因为它会自动检测到当前虚拟环境。如果你想快速在当前目录对应的虚拟环境中执行命令,可以使用 poetry run <你的命令> 命令,比如:

    $ poetry run python app.py

    如果你想显式的激活虚拟环境,使用 poetry shell 命令:

    $ poetry shell

    追踪 & 更新包

    使用 poetry show 命令可以查看所有安装的依赖(可以传递包名称作为参数查看具体某个包的信息):

    $ poetry show

    添加 –tree 选项可以查看依赖关系:

    $ poetry show --tree

    添加 –outdated 可以查看可以更新的依赖:

    $ poetry show --outdated

    执行 poetry update 命令可以更新所有锁定版本的依赖:

    $ poetry update

    如果你想更新某个指定的依赖,传递包名作为参数:

    $ poetry update foo

    卸载包

    使用 poetry remove <包名称> 卸载一个包:

    $ poetry remove foo

    自定义镜像源

    在项目根目录文件pyproject.toml末尾加入下述其一:

    阿里云:

    [[tool.poetry.source]]
    name = "aliyun"
    url = "https://mirrors.aliyun.com/pypi/simple/"

    豆瓣:

    [[tool.poetry.source]]
    name = "douban"
    url = "https://pypi.doubanio.com/simple/"

    网易:

    [[tool.poetry.source]]
    name = "163"
    url = "https://mirrors.163.com/pypi/simple/"

    清华大学:

    [[tool.poetry.source]]
    name = "tsinghua"
    url = "https://pypi.tuna.tsinghua.edu.cn/simple/"

    关于离线安装

    网上介绍的离线安装方法,但是实测无法安装,可能是官方已经不支持了。

  • 下载install-poetry.py,链接:https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py
  • 下载poetry安装包,链接:https://github.com/python-poetry/poetry/releases/download/1.1.13/poetry-1.1.13-win32.tar.gz
  • 安装poetry

    python install-poetry.py --file poetry-1.1.13-win32.tar.gz

    将 %USERPROFILE%\.poetry\bin 添加到系统环境变量path。

    来源:天天开心学编程

    物联沃分享整理
    物联沃-IOTWORD物联网 » Python 依赖管理和打包工具 Poetry 安装和测试

    发表回复