最后活跃于 10 months ago

這段程式碼適合用於開發或部署 Streamlit 應用時,確保運行的 Python 版本符合 Streamlit 的支援範圍,避免因版本不符而導致問題。

修订 409cdaf593b990d3f6ddb4f3ce9e2d531bc322cc

python_version_checker.py 原始文件
1import sys
2
3class PythonVersionChecker:
4 def __init__(self, supported_versions):
5 self.supported_versions = supported_versions
6
7 def get_current_version(self):
8 """取得當前的 Python 版本。"""
9 return sys.version_info[:2]
10
11 def is_supported(self):
12 """檢查當前的 Python 版本是否在支援的版本列表中。"""
13 current_version = self.get_current_version()
14 return current_version in self.supported_versions
15
16class StreamlitVersionChecker(PythonVersionChecker):
17 def __init__(self):
18 # 定義 Streamlit 支援的 Python 版本
19 supported_versions = [(3, 9), (3, 10), (3, 11), (3, 12), (3, 13)]
20 super().__init__(supported_versions)
21
22 def check_version(self):
23 """檢查並輸出當前的 Python 版本是否受 Streamlit 支援。"""
24 current_version = self.get_current_version()
25 if self.is_supported():
26 print(f"當前的 Python 版本 {current_version[0]}.{current_version[1]} 受 Streamlit 支援。")
27 else:
28 print(f"當前的 Python 版本 {current_version[0]}.{current_version[1]} 不受 Streamlit 支援。")
29
30# 使用範例
31checker = StreamlitVersionChecker()
32checker.check_version()
33
34