Github Actions无法找到适配Python3.9的指定版本依赖库
解决Python依赖安装失败问题(AQI数据流水线场景)
问题背景
我搭建了一套获取AQI数据并存储为CSV的Python处理流水线,搭配GitHub Actions实现定时运行,但在依赖安装阶段持续失败。尝试过更换操作系统、升级pip、更新setuptools/wheel、清理pip缓存等方法,问题依然存在。
流水线脚本代码
import requests import time from datetime import datetime, timedelta import csv import sys from tqdm import tqdm from dotenv import load_dotenv import os # Load environment variables load_dotenv() # Get API configurations API_KEY = os.getenv('API_KEY') BASE_URL = os.getenv('BASE_URL') # Coordinates for Baku LAT = 40.3777 LON = 49.8920 # Date range for data fetching END_DATE = datetime.now() START_DATE = datetime(2022, 1, 13) def fetch_daily_data(date): """ Fetch air quality data for a specific date Args: date (datetime): Date to fetch data for Returns: dict or None: JSON response or None if error """ try: params = { "lat": LAT, "lon": LON, "key": API_KEY, "start_date": date.strftime('%Y-%m-%d'), "end_date": (date + timedelta(days=1)).strftime('%Y-%m-%d'), "tz": "local" } response = requests.get(BASE_URL, params=params) response.raise_for_status() # Raise an exception for bad status codes return response.json() except requests.RequestException as e: print(f"Error fetching data for {date.strftime('%Y-%m-%d')}: {e}") return None def main(): # CSV file configuration csv_file = "baku_air_quality_data.csv" csv_headers = ["timestamp", "aqi", "o3", "so2", "no2", "co", "pm25", "pm10"] total_days = (END_DATE - START_DATE).days + 1 try: with open(csv_file, "w", newline='', encoding='utf-8') as file: writer = csv.writer(file) writer.writerow(csv_headers) pbar = tqdm(total=total_days, desc="Fetching data", unit="day") current_date = START_DATE while current_date <= END_DATE: daily_data = fetch_daily_data(current_date) if daily_data and 'data' in daily_data: for hour_data in daily_data['data']: row = [ hour_data.get('timestamp_local', ''), hour_data.get('aqi', ''), hour_data.get('o3', ''), hour_data.get('so2', ''), hour_data.get('no2', ''), hour_data.get('co', ''), hour_data.get('pm25', ''), hour_data.get('pm10', '') ] writer.writerow(row) current_date += timedelta(days=1) pbar.update(1) time.sleep(1) # Respect API rate limits pbar.close() print(f"Data saved to {csv_file}") except Exception as e: print(f"An error occurred: {e}") sys.exit(1) if __name__ == "__main__": main()
GitHub Actions工作流配置
name: Run Script Every 3 Days on: push jobs: run-script: runs-on: ubuntu-latest steps: - name: Checkout Repository uses: actions/checkout@v3 - name: Set Up Python uses: actions/setup-python@v4 with: python-version: 3.9 - name: Install Dependencies env: API_KEY: ${{ secrets.API_KEY }} run: | pip install -r requirements.txt # If your script has dependencies - name: Run the Script run: | python feature_pipeline.py # Replace with the name of your script
错误信息
ERROR: Could not find a version that satisfies the requirement ipython==8.29.0 (from versions: 0.10, 0.10.1, 0.10.2, 0.11, 0.12, 0.12.1, 0.13, 0.13.1, 0.13.2, ...) ERROR: No matching distribution found for ipython==8.29.0
更换ipython版本后,其他库也会出现类似的安装失败问题。
当前requirements.txt内容
appnope==0.1.4 asttokens==2.4.1 awscli==1.36.1 boto3==1.35.60 botocore==1.35.60 certifi==2024.8.30 charset-normalizer==3.4.0 colorama==0.4.6 comm==0.2.2 contourpy==1.3.0 cycler==0.12.1 debugpy==1.8.7 decorator==5.1.1 docutils==0.16 executing==2.1.0 fonttools==4.54.1 idna==3.10 ipykernel==6.29.5 ipython==8.29.0 jedi==0.19.1 jmespath==1.0.1 joblib==1.4.2 jupyter_client==8.6.3 jupyter_core==5.7.2 kiwisolver==1.4.7 matplotlib==3.9.2 matplotlib-inline==0.1.7 nest-asyncio==1.6.0 numpy==2.1.2 packaging==24.1 pandas==2.2.3 parso==0.8.4 patsy==0.5.6 pexpect==4.9.0 pillow==11.0.0 platformdirs==4.3.6 prompt_toolkit==3.0.48 psutil==6.1.0 ptyprocess==0.7.0 pure_eval==0.2.3 pyarrow==18.0.0 pyasn1==0.6.1 Pygments==2.18.0 pyparsing==3.2.0 python-dateutil==2.9.0.post0 python-dotenv==1.0.1 pytz==2024.2 PyYAML==6.0.2 pyzmq==26.2.0 requests==2.32.3 rsa==4.7.2 s3transfer==0.10.3 scikit-learn==1.5.2 scipy==1.14.1 seaborn==0.13.2 setuptools==75.5.0 six==1.16.0 stack-data==0.6.3 statsmodels==0.14.4 threadpoolctl==3.5.0 tornado==6.4.1 tqdm==4.66.5 traitlets==5.14.3 tzdata==2024.2 urllib3==2.2.3 wcwidth==0.2.13 wheel==0.45.0
解决方案
1. 精简依赖列表
你的脚本仅依赖requests、tqdm、python-dotenv三个第三方库,其余如ipython、jupyter、matplotlib、awscli等均为无关依赖,会增加安装复杂度与版本冲突风险。将requirements.txt替换为:
requests>=2.32.0 tqdm>=4.66.0 python-dotenv>=1.0.0
2. 适配Python 3.9版本
你在Actions中使用Python 3.9,但部分库的高版本已不再支持该版本(例如ipython 8.29.0要求Python 3.10+)。若必须保留某些非必要库,需指定兼容3.9的版本,例如将ipython==8.29.0改为ipython==8.12.3(最后一个支持Python 3.9的版本)。
3. 优化Actions安装步骤
在安装依赖前先升级pip、setuptools和wheel,提升依赖解析能力,同时移除安装阶段无关的环境变量:
- name: Install Dependencies run: | python -m pip install --upgrade pip setuptools wheel pip install -r requirements.txt
4. 解决网络下载问题
若因网络限制导致包下载失败,可在pip安装时指定国内镜像源:
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
内容的提问来源于stack exchange,提问作者Parviz Pirizade
相关产品推荐
相关产品推荐

