1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

指定ライブラリーに対応可能なpythonのバージョンを調べるスクリプト

Posted at

目的

python処理系をバージョンアップしようかと思った時、使ってる又は使いたいライブラリーが対応できているかが気になる。
その確認のためのスクリプトを作成した。

対象

pypiで、そのライブラリーのjsonデータの"Classifiers" セクションで対応バージョンが記述されているライブラリーのみが以下のスクリプトで調べることができる。

コード

import json
import requests
import re

def get_latest_python_version(package_name):
    url = f"https://pypi.org/pypi/{package_name}/json"
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        classifiers = data.get('info', {}).get('classifiers', [])
        version = set()
        for classifier in classifiers:
            m = re.search(r"Programming Language :: Python ::\s*(\d\S*)$" , classifier)
            if m is not None:
                v = m.group(1)
                # version |= {v}
                version.add(v)
        if version:
            return version
    raise ValueError(f"Could not find classifiers data for {package_name}")

def applicable_python_version(pkg_names):
    V = []
    for pkg in pkg_names:
        V.append(get_latest_python_version(pkg))
    S = V[0]
    S.intersection_update(*V[1:])
    return S

使用例

applicable_python_version(["numpy","scipy","pandas", "xlwings"])
# --> {'3', '3.10', '3.11', '3.9'}
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?