我必须在Windows服务器上运行Python脚本。我怎么知道我用的是哪个版本的Python,这真的很重要吗?

我在考虑升级到最新版本的Python。


当前回答

在Windows操作系统下,在命令提示符中输入以下命令,验证命令的Python版本。

c:\> python -V
Python 2.7.16

c:\> py -2 -V
Python 2.7.16

c:\> py -3 -V
Python 3.7.3

此外,要查看每个Python版本的文件夹配置,请运行以下命令:

For Python 2, 'py -2 -m site'
For Python 3, 'py -3 -m site'

其他回答

Windows下Python的默认版本和所有已安装版本的路径:

py -0p

一行程序:

❯❯  python -V | cut -c8-
3.11.0

❯❯ ~ python -VV
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]

❯❯ ~ python --version
Python 3.11.0

❯❯ ~ py --list
-V:3.11 *        Python 3.11 (64-bit)
-V:3.10          Python 3.10 (64-bit)
-V:3.9           Python 3.9 (64-bit)

❯❯ ~ py -V
Python 3.11.0

❯❯ ~ py -VV
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]

❯❯ ~ py --version
Python 3.11.0

❯❯ ~ py -0p
-V:3.11 *        W:\Windows 10\Python311\python.exe
-V:3.10          W:\Windows 10\Python310\python.exe
-V:3.9           C:\Program Files\Python39\python.exe

❯❯ ~ python -c 'import sys; print(".".join(sys.version.split(".")[0:2]))'
3.11

❯❯ ~ python -c 'import sys; print(sys.version)'
3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]

❯❯ ~ python -c 'import sys; print((str(sys.version_info.major) +"."+ str(sys.version_info.minor)))'
3.11

❯❯ ~ python -c 'import sys; print(sys.version_info)' sys.version_info(major=3, minor=11, micro=0, releaselevel='final', serial=0)

❯❯ ~ python -c 'import platform; print(platform.python_version()[:-2])'
3.11

❯❯ ~ python -c 'import platform; print(platform.python_version())'
3.11.0

❯❯ ~ python -c 'import platform; print("{0[0]}.{0[1]}".format(platform.python_version_tuple()))'
3.11

❯❯ ~ python -c 'import platform; print(platform.python_version_tuple())'
('3', '11', '0')

Python 2.5 +:

python --version

Python 2.4 -:

python -c 'import sys; print(sys.version)'

我在Windows 10上安装了Python 3.7.0。

这是我在命令提示符和Git Bash中工作的方法:

使用实例运行Python并查看版本。

py

只检查您拥有的版本:

py --version

or

py -V    # Make sure it is a capital V

注意:python, python——version, python -V, python, python——version, python -V不适合我。

对于bash脚本,这将是最简单的方法:

# In the form major.minor.micro e.g. '3.6.8'
# The second part excludes the 'Python ' prefix 
PYTHON_VERSION=`python3 --version | awk '{print $2}'`
echo "python3 version: ${PYTHON_VERSION}"
python3 version: 3.6.8

如果你只是需要专业。次要版本(例如3.6),您可以使用上述,然后选择前3个字符:

PYTHON_VERSION=`python3 --version | awk '{print $2}'`
echo "python3 major.minor: ${PYTHON_VERSION:0:3}"
python3 major.minor: 3.6

or

PYTHON_VERSION=`python3 -c 'import sys; print(str(sys.version_info[0])+"."+str(sys.version_info[1]))'`
echo "python3 major.minor: ${PYTHON_VERSION}"
python3 major.minor: 3.6

在Python IDE中,只需复制并粘贴以下代码并运行它(版本将出现在输出区域):

import sys
print(sys.version)