如何在Travis CI中爲Windows, Linux和macOS配置Python環境

Travis CI提供了很多虛擬機環境,但是沒有想象中方便。比如要運行Python,只有Linux和macOS是自帶的,Windows需要安裝。不同的操作系統配置方式也不同,少不了一番折騰。

Travis CI的Python 3.x環境搭建

Linux自帶Python,非常方便,只需要設置Python版本即可。CPU可以選擇AMD64和ARM64:

jobs:
  include:
    - name: "Python 3.6.0 on AMD64 Linux"
      python: 3.6
      os: linux
      arch: amd64
    - name: "Python 3.7.0 on AMD64 Linux"
      python: 3.7 
      os: linux
      arch: amd64
    - name: "Python 3.8.0 on AMD64 Linux"
      python: 3.8 
      os: linux
      arch: amd64
    - name: "Python 3.9.0 on AMD64 Linux"
      python: 3.9  
      os: linux
      arch: amd64
    - name: "Python 3.6.0 on ARM64 Linux"
      python: 3.6
      os: linux
      arch: arm64
    - name: "Python 3.7.0 on ARM64 Linux"
      python: 3.7 
      os: linux
      arch: arm64
    - name: "Python 3.8.0 on ARM64 Linux"
      python: 3.8 
      os: linux
      arch: arm64
    - name: "Python 3.9.0 on ARM64 Linux"
      python: 3.9    
      os: linux
      arch: arm64

macOS需要設置Xcode來選擇對應的鏡像。Xcode自帶Python,但不清楚具體的版本。可以用python --version來查看。macOS也支持brew安裝,但是非常的慢。Python 3.6不支持。

    - name: "Python 3.7 on macOS"
      os: osx
      osx_image: xcode11.4 
      language: shell    
      before_install:
        - python3 --version  
    - name: "Python 3.8 on macOS"
      os: osx
      osx_image: xcode11.6 
      language: shell    
      before_install:
        - python3 --version
    - name: "Python 3.9 on macOS"
      os: osx
      osx_image: xcode12.2 
      language: shell  
      before_install:
        - python3 --version    

Windows上需要通過choco安裝Python,然後配置環境變量:

    - name: "Python 3.6.0 on Windows"
      os: windows           
      language: shell       
      before_install:
        - choco install python --version 3.6.0
        - python -m pip install --upgrade pip
      env: PATH=/c/Python36:/c/Python36/Scripts:$PATH
    - name: "Python 3.7.0 on Windows"
      os: windows           
      language: shell       
      before_install:
        - choco install python --version 3.7.0
        - python -m pip install --upgrade pip
      env: PATH=/c/Python37:/c/Python37/Scripts:$PATH
    - name: "Python 3.8.0 on Windows"
      os: windows           
      language: shell       
      before_install:
        - choco install python --version 3.8.0
        - python -m pip install --upgrade pip
      env: PATH=/c/Python38:/c/Python38/Scripts:$PATH
    - name: "Python 3.9.0 on Windows"
      os: windows           
      language: shell       
      before_install:
        - choco install python --version 3.9.0
        - python -m pip install --upgrade pip
      env: PATH=/c/Python39:/c/Python39/Scripts:$PATH

安裝Python依賴庫。如果你需要OpenCV,注意Windows上要使用opencv-python-headless。不然會返回DLL找不到的錯誤:

install:
    - if [[ ${TRAVIS_OS_NAME} == "windows" ]]; then
        pip install dbr opencv-python-headless;
      else
        pip3 install dbr opencv-python;
      fi

最後執行測試腳本:

branches:
  only:
    - main

script:
    - python3 ./test.py ./images/1D-1-OverExposure.jpg || python ./test.py ./images/1D-1-OverExposure.jpg

查看任務狀態:

在這裏插入圖片描述

源碼

https://github.com/Dynamsoft/python-gui-barcode-reader/blob/main/.travis.yml

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章