Poetry - 實用的套件管理工具

使用心得

自己覺得一個不錯的虛擬環境控管工具,推薦給大家

使用起來像是以一個專案(資料夾)為單位,在資料夾裡面對套件的安裝做操作。

同個資料夾可以同時有不同版本的 python 安裝環境。

poetry 主要有兩個檔案

  • pyproject.toml
  • pyproject.toml

每次對套件有所修改時,都會自動更新檔案,不用怕忘記把套件 dump 出來!

安裝

可以參考 官往文件

以下為 Ubuntu 的例子

$ curl -sSL <https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py> | python3 -
# 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 file located at:
$HOME/.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.6
- Downloading poetry-1.1.6-linux.tar.gz (72.33MB)
Poetry (1.1.6) 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
$ $HOME/.poetry/bin
>>> bash: /home/seraphine/.poetry/bin: Is a directory

啟動 poetry 環境

啟用 poetry 才能使用 poetry command

$ source $HOME/.poetry/env

將以上環境變數加入 ~/.bashrc 檔案中,這樣在 termainl 輸入 poetry 時才會有此 command

打開 ~/.bashrc

$ vim ~/.bashrc

在最底下加入

source $HOME/.poetry/env

查看版本

$ poetry --version

建立專案

  • 新的專案
    $ poetry new poetry-demo
  • 已有專案
    $ cd pre-existing-project
    $ poetry init

新增套件

  • 如果套件有在 poetry.lock 和 pyproject.toml 裡面,直接執行

    $ poetry install

    即使沒有也可以執行,會安裝預設裡的套件

  • 新增,更新,降版套件

    $ poetry add <套件名稱>=<版本號>
  • 移除套件

    $ poetry remove <套件名稱>
  • 用 requirements.txt 裝載套件

    $ cat requirements.txt|xargs poetry add
  • 查看 poetry 中的套件

    $ poetry show
  • 在 poetry 環境中 run 程式

    $ poetry run python3 hello.py

在 poetry 中建立不同版本的 python 環境

個人覺得這個功能很強大
如果今天我想把專案的 python 版本做升級,一樣在同一個資料夾裡面,同一個檔案就可以做到管理!
  1. 先將 pyproject.toml 檔裡面修改 python 版本

    [tool.poetry.dependencies]
    python = "^3.6" << 這個地方改成要的 python 版本
    requests = "^2.25.1"
  2. 新增所需之 python 版本

    $ poetry env use poython3.6
  3. 查看目前 poetry 環境有的 python 版本

    $ poetry env list
  4. 有兩種方法可以指定 py 檔使用之 python 版本

    • 方法一 直接指定 python 版本執行

      $ poetry run python3.6 檔案名稱
    • 方法二 啟動虛擬環境後執行

      1. 查看 activate 的 python 版本之虛擬環境路徑

        $ poetry env info -p
      2. 啟動該路徑之虛擬環境

        • 假設上面的路徑是

          >>> /home/seraphine/.cache/pypoetry/virtualenvs/poetry-test-CI7KfrHu-py3.9
        • 則輸入

          $ source /home/seraphine/.cache/pypoetry/virtualenvs/poetry-test-CI7KfrHu-py3.9/bin/activate
      3. 離開虛擬環境

        $ deactivate

啟動 poetry 虛擬環境

啟動的方式有兩種

  1. 一種是如上面所說,查看 activate 的 python 版本之虛擬環境路徑

    $ poetry env info -p

    而後

    $ source /home/seraphine/.cache/pypoetry/virtualenvs/poetry-test-CI7KfrHu-py3.9/bin/activate
  2. 第二種是直接在資料夾內下

    $ poetry shell

    之後如果想安裝套件,可以下 poetry shell command,打開虛擬環境後(termainal 中的 command line 會在最前面顯示"(poetry-test-CI7KfrHu-py3.9)"),使用 pip install 安裝套件

  3. 此情況多出現在用 .whl 檔安裝套件,或是需要安裝指令很特別,如 pip install tritonclient[all] 等情況下使用。

reference

Comments

Popular posts from this blog

《 Imgproxy 使用分析一:圖片下載速度優化分析:Akamai CDN vs Imgproxy 效能比較》

《 Akamai + S3 與 CloudFront + Imgproxy + S3 使用分析二:壓縮圖片設計流程:檔案大小 vs 載入時間的權衡》

程式語言初學者 Docker 入門第二章 —— 安裝與驗證 (Mac)