怎样判断当前py文件在什么版本的python环境下运行?
迷茫
迷茫 2017-04-17 11:02:54
[Python讨论组]

搜关键词搜不准,都是搜到直接在shell环境下运行python -v,关键是我要在程序里用到版本信息做判断呢,该怎么写呢?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(2)
高洛峰

查下manual就可以知道了,有多个类似变量可用

sys.version 
A string containing the version number of the Python interpreter plus additional information on the build number and compiler used. This string is displayed when the interactive interpreter is started. Do not extract version information out of it, rather, use version_info and the functions provided by the platform module.
PHPz

如果是给人读,用 sys.version
如果是给机器比较,用 sys.version_info
如果是判断是不是 PyPy 等第三方实现,用 sys.implementation(要 Python 3.3)。

例子:

import sys
if sys.version_info < (3, 4):
    raise RuntimeError('At least Python 3.4 is required')
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号