python - 在 shell 脚本中检测 python 版本

标签 python shell

我想检测一下Linux系统上是否安装了python,如果是,安装了哪个python版本。

我该怎么做?还有什么比解析"python --version"的输出更优雅的吗?

最佳答案

您可以使用以下几行:

$ python -c 'import sys; print(sys.version_info[:])'
(2, 6, 5, 'final', 0)

元组已记录 here .您可以扩展上面的 Python 代码,以适合您要求的方式格式化版本号,或者实际上对其执行检查。

您需要在脚本中检查 $? 以处理未找到 python 的情况。

附:我使用稍微奇怪的语法来确保与 Python 2.x 和 3.x 的兼容性。

关于python - 在 shell 脚本中检测 python 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6141581/

相关文章:

linux - shell脚本中for循环的迭代

bash - 通过Oozie Shell脚本运行配置单元查询时未找到表异常

python - Pandas Dataframes 不同列的总和值计数

python - 类型错误 : 'module' object is not callable while opening chrome browser using selenium webdriver

python - Flask-restful:将复杂对象编码为 json

linux - shell 语法错误

linux - 删除 shell 命令后的换行符

linux - 遍历带路径的连接字符串 - 防止通配

python - 使用python处理来自多个目录的同名数据

python - MemoryError 将两个数据帧与 pandas 和 dasks 合并——我该怎么做?