我必须在Windows服务器上运行Python脚本。我怎么知道我用的是哪个版本的Python,这真的很重要吗?
我在考虑升级到最新版本的Python。
我必须在Windows服务器上运行Python脚本。我怎么知道我用的是哪个版本的Python,这真的很重要吗?
我在考虑升级到最新版本的Python。
当前回答
要在Jupyter笔记本中检查Python版本,您可以使用:
from platform import python_version
print(python_version())
获取版本号,如下:
3.7.3
or:
import sys
print(sys.version)
为了获得更多的信息,如
3.7.3 (default, Apr 24 2019, 13:20:13) [MSC v.1915 32 bit (Intel)]
or:
sys.version_info
要获得大版本、小版本和微版本,如
sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)
其他回答
虽然问题是“我使用的是哪个版本?”,但这可能并不是你需要知道的全部内容。您可能已经安装了其他版本,这可能会导致问题,特别是在安装其他模块时。这是我粗略地找出安装了什么版本的方法:
updatedb # Be in root for this
locate site.py # All installations I've ever seen have this
单个Python安装的输出应该是这样的:
/usr/lib64/python2.7/site.py
/usr/lib64/python2.7/site.pyc
/usr/lib64/python2.7/site.pyo
多个安装将会有类似这样的输出:
/root/Python-2.7.6/Lib/site.py
/root/Python-2.7.6/Lib/site.pyc
/root/Python-2.7.6/Lib/site.pyo
/root/Python-2.7.6/Lib/test/test_site.py
/usr/lib/python2.6/site-packages/site.py
/usr/lib/python2.6/site-packages/site.pyc
/usr/lib/python2.6/site-packages/site.pyo
/usr/lib64/python2.6/site.py
/usr/lib64/python2.6/site.pyc
/usr/lib64/python2.6/site.pyo
/usr/local/lib/python2.7/site.py
/usr/local/lib/python2.7/site.pyc
/usr/local/lib/python2.7/site.pyo
/usr/local/lib/python2.7/test/test_site.py
/usr/local/lib/python2.7/test/test_site.pyc
/usr/local/lib/python2.7/test/test_site.pyo
python -V
http://docs.python.org/using/cmdline.html#generic-options
——version也可以工作(在2.5版本中引入)
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')
对于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
只需打开命令提示符(Win + R)并键入cmd,然后在命令提示符中输入python将为您提供有关版本的所有必要信息: