Windows 下多版本 Python 共享 Poetry(qbit)

qbit發表於2023-03-27

前言

  • 技術棧

    Windows 10
    Python  3.8.10
    Python  3.11.2
    pip     23.0.1
    pipx    1.2.0
    poetry  1.4.1
  • Python 3.8 安裝目錄

    C:\Python38
  • Python 3.11 安裝目錄

    C:\Python311

安裝 poetry

  • 設定將以下路徑加入 path 環境變數

    C:\Python38
    C:\Python38\Scripts
    C:\Python311
    C:\Python311\Scripts
    C:\Users\qbit\.local\bin\
  • 複製檔案

    C:\Python38\python.exe -> C:\Python38\py38.exe 
    C:\Python311\python.exe -> C:\Python38\py311.exe 
  • 設定 pip 國內映象源

    py38 -m pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
    py311 -m pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
  • 升級 pip

    py38 -m pip install pip --upgrade
    py311 -m pip install pip --upgrade
  • 安裝或升級 pipx

    py38 -m pip install pipx --upgrade
    py311 -m pip install pipx --upgrade
  • 在 Python 3.11 下用 pipx 安裝 Poetry

    py311 -m pipx install poetry --force -i https://mirrors.aliyun.com/pypi/simple/
  • 檢視安裝情況

    > where poetry
    c:\Users\qbit\.local\bin\poetry.exe
    > poetry --version
    Poetry (version 1.4.1)
  • 設定將虛擬環境目錄放在專案內

    poetry config virtualenvs.in-project true

測試多版本共享

測試 Python 3.11

  • 建立空目錄 F:\tmp\test311,在裡面建立檔案 pyproject.toml,檔案內容如下:

    [tool.poetry]
    name = "test"
    version = "0.1.0"
    description = ""
    authors = ["qbit"]
    readme = "README.md"
    
    [[tool.poetry.source]]
    name = "aliyun"
    url = "https://mirrors.aliyun.com/pypi/simple/"
    default = true
    
    [tool.poetry.dependencies]
    python = "^3.11"
    requests = "~2.28.2"
    
    [build-system]
    requires = ["poetry-core"]
    build-backend = "poetry.core.masonry.api"
  • 建立虛擬環境並安裝第三方庫

    poetry update -vv
  • 檢視虛擬環境資訊

    > poetry env info
    
    Virtualenv
    Python:         3.11.2
    Implementation: CPython
    Path:           F:\tmp\test311\.venv
    Executable:     F:\tmp\test311\.venv\Scripts\python.exe
    Valid:          True
    
    System
    Platform:   win32
    OS:         nt
    Python:     3.11.2
    Path:       C:\Python311
    Executable: C:\Python311\python.exe

    測試 Python 3.8

  • 建立空目錄 F:\tmp\test38,在裡面建立檔案 pyproject.toml,檔案內容如下:

    [tool.poetry]
    name = "test"
    version = "0.1.0"
    description = ""
    authors = ["qbit"]
    readme = "README.md"
    
    [[tool.poetry.source]]
    name = "aliyun"
    url = "https://mirrors.aliyun.com/pypi/simple/"
    default = true
    
    [tool.poetry.dependencies]
    python = "^3.8"
    requests = "~2.28.2"
    
    [build-system]
    requires = ["poetry-core"]
    build-backend = "poetry.core.masonry.api"
  • 切換 python 版本並建立虛擬環境

    poetry env use C:\Python38\python.exe
  • 檢視虛擬環境資訊

    > poetry env info
    
    Virtualenv
    Python:         3.8.10
    Implementation: CPython
    Path:           F:\tmp\test38\.venv
    Executable:     F:\tmp\test38\.venv\Scripts\python.exe
    Valid:          True
    
    System
    Platform:   win32
    OS:         nt
    Python:     3.8.10
    Path:       C:\Python38
    Executable: C:\Python38\python.exe
  • 安裝第三方庫

    poetry update -vv

後記

  • 如果報類似如下錯誤,部分包安裝不成功,可以 poetry shell 進入虛擬環境後用 pip 安裝報錯的包

    _WheelFileValidationError
    version.py is not mentioned in RECORD
    In {self._zipfile.filename}, hash / size of {item.filename} didn't match RECORD
  • 出現上面這個錯誤的原因是第三方包不夠規範,使得 poetry 在校驗的時候出錯。可以參考 installer: do not fail on invalid wheels, print only a warning
本文出自 qbit snap

相關文章