在 Tcl 脚本中查找 python 版本
Find python version in Tcl script
是否可以在 .tcl 脚本中找到安装的 python 版本?换句话说,我如何从 .tcl 脚本中判断默认路径中的 python 版本?
Tcl Wiki 不包含关于此的有用信息
目前我正在调用一个 python 脚本,该脚本打印 sys.version 并解析其输出。
.py
import sys
def find_version():
version = sys.version
version = version.split()[0].split('.')
version = version[0] + '.' + version[1]
print(version)
if __name__ == '__main__':
find_version()
.tcl
set file "C://find_python_version.py"
set output [exec python $file]
一个足够简单的方法似乎是解析 python --version
:
的结果
proc pythonVersion {{pythonExecutable "python"}} {
# Tricky point: Python 2.7 writes version info to stderr!
set info [exec $pythonExecutable --version 2>@1]
if {[regexp {^Python ([\d.]+)$} $info --> version]} {
return $version
}
error "failed to parse output of $pythonExecutable --version: '$info'"
}
在此系统上测试:
% pythonVersion
3.6.8
% pythonVersion python2.7
2.7.15
我觉得不错。
我会使用 Python 的 sys.version_info
因为我可以用任何我喜欢的方式格式化版本字符串:
set pythonVersion [exec python -c {import sys; print("%d.%d.%d" % sys.version_info[:3])}]
puts "Python version: $pythonVersion"
输出:
Python版本:2.7.15
一些注意事项:
- Python 脚本(在大括号中)跟在
-c
标志之后将以 x.y.z 的形式打印出版本,您可以按照自己喜欢的方式格式化它
sys.version_info
的值是许多元素的列表,请参阅文档。我只对前 3 个元素感兴趣,因此 sys.version_info[:3]
- 带括号的
print
statement/function 适用于 Python 2 和 Python 3
是否可以在 .tcl 脚本中找到安装的 python 版本?换句话说,我如何从 .tcl 脚本中判断默认路径中的 python 版本?
Tcl Wiki 不包含关于此的有用信息
目前我正在调用一个 python 脚本,该脚本打印 sys.version 并解析其输出。
.py
import sys
def find_version():
version = sys.version
version = version.split()[0].split('.')
version = version[0] + '.' + version[1]
print(version)
if __name__ == '__main__':
find_version()
.tcl
set file "C://find_python_version.py"
set output [exec python $file]
一个足够简单的方法似乎是解析 python --version
:
proc pythonVersion {{pythonExecutable "python"}} {
# Tricky point: Python 2.7 writes version info to stderr!
set info [exec $pythonExecutable --version 2>@1]
if {[regexp {^Python ([\d.]+)$} $info --> version]} {
return $version
}
error "failed to parse output of $pythonExecutable --version: '$info'"
}
在此系统上测试:
% pythonVersion
3.6.8
% pythonVersion python2.7
2.7.15
我觉得不错。
我会使用 Python 的 sys.version_info
因为我可以用任何我喜欢的方式格式化版本字符串:
set pythonVersion [exec python -c {import sys; print("%d.%d.%d" % sys.version_info[:3])}]
puts "Python version: $pythonVersion"
输出: Python版本:2.7.15
一些注意事项:
- Python 脚本(在大括号中)跟在
-c
标志之后将以 x.y.z 的形式打印出版本,您可以按照自己喜欢的方式格式化它 sys.version_info
的值是许多元素的列表,请参阅文档。我只对前 3 个元素感兴趣,因此sys.version_info[:3]
- 带括号的
print
statement/function 适用于 Python 2 和 Python 3