在输出中打印Python版本

问题:在输出中打印Python版本

如何从脚本中打印当前Python安装的版本号?

How can I print the version number of the current Python installation from my script?


回答 0

尝试

import sys
print(sys.version)

这将打印完整的版本信息字符串。如果您只想要python版本号,那么BastienLéonard的解决方案是最好的。您可能要检查完整的字符串,看看是否需要它或其中的一部分。

Try

import sys
print(sys.version)

This prints the full version information string. If you only want the python version number, then Bastien Léonard’s solution is the best. You might want to examine the full string and see if you need it or portions of it.


回答 1

import platform
print(platform.python_version())

这打印像

3.7.2

import platform
print(platform.python_version())

This prints something like

3.7.2


回答 2

尝试

python --version 

要么

python -V

这将在终端中返回当前的python版本。

Try

python --version 

or

python -V

This will return a current python version in terminal.


回答 3

import sys  

扩展版

sys.version_info  
sys.version_info(major=3, minor=2, micro=2, releaselevel='final', serial=0)

具体

maj_ver = sys.version_info.major  
repr(maj_ver) 
'3'  

要么

print(sys.version_info.major)
'3'

要么

version = ".".join(map(str, sys.version_info[:3]))
print(version)
'3.2.2'
import sys  

expanded version

sys.version_info  
sys.version_info(major=3, minor=2, micro=2, releaselevel='final', serial=0)

specific

maj_ver = sys.version_info.major  
repr(maj_ver) 
'3'  

or

print(sys.version_info.major)
'3'

or

version = ".".join(map(str, sys.version_info[:3]))
print(version)
'3.2.2'

回答 4

如果您正在使用jupyter笔记本,请尝试:

!python --version

如果使用终端,请尝试:

 python --version

If you are using jupyter notebook Try:

!python --version

If you are using terminal Try:

 python --version