我安装了哪个版本的 Python?

发布于 2024-12-27 18:06:52 字数 95 浏览 2 评论 0原文

我必须在 Windows 服务器上运行 Python 脚本。我如何知道我拥有哪个版本的 Python,这真的很重要吗?

我正在考虑更新到最新版本的Python。

I have to run a Python script on a Windows server. How can I know which version of Python I have, and does it even really matter?

I was thinking of updating to the latest version of Python.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(27

执着的年纪 2025-01-03 18:06:53

Python 2.5+:

python --version

Python 2.4-:

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

Python 2.5+:

python --version

Python 2.4-:

python -c 'import sys; print(sys.version)'
倒带 2025-01-03 18:06:53

在命令提示符处键入:

python -V

或者如果您有 pyenv:

pyenv versions

At a command prompt type:

python -V

Or if you have pyenv:

pyenv versions
北音执念 2025-01-03 18:06:53

尽管问题是“我正在使用哪个版本?”,但这实际上可能并不是您需要了解的全部内容。您可能安装了其他版本,这可能会导致问题,特别是在安装附加模块时。这是我查明已安装版本的粗略方法:

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

Although the question is "which version am I using?", this may not actually be everything you need to know. You may have other versions installed and this can cause problems, particularly when installing additional modules. This is my rough-and-ready approach to finding out what versions are installed:

updatedb                  # Be in root for this
locate site.py            # All installations I've ever seen have this

The output for a single Python installation should look something like this:

/usr/lib64/python2.7/site.py
/usr/lib64/python2.7/site.pyc
/usr/lib64/python2.7/site.pyo

Multiple installations will have output something like this:

/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
樱花细雨 2025-01-03 18:06:53

当我打开 Python(命令行) 时,它告诉我的第一件事就是版本。

When I open Python (command line) the first thing it tells me is the version.

甜扑 2025-01-03 18:06:53
In [1]: import sys

In [2]: sys.version
2.7.11 |Anaconda 2.5.0 (64-bit)| (default, Dec  6 2015, 18:08:32) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]

In [3]: sys.version_info
sys.version_info(major=2, minor=7, micro=11, releaselevel='final', serial=0)

In [4]: sys.version_info >= (2,7)
Out[4]: True

In [5]: sys.version_info >= (3,)
Out[5]: False
In [1]: import sys

In [2]: sys.version
2.7.11 |Anaconda 2.5.0 (64-bit)| (default, Dec  6 2015, 18:08:32) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]

In [3]: sys.version_info
sys.version_info(major=2, minor=7, micro=11, releaselevel='final', serial=0)

In [4]: sys.version_info >= (2,7)
Out[4]: True

In [5]: sys.version_info >= (3,)
Out[5]: False
数理化全能战士 2025-01-03 18:06:53

简而言之:

在命令提示符中输入python

只需打开命令提示符 (Win + R) 并键入 cmd 并在命令提示符然后输入 python 将为您提供有关版本的所有必要信息:

Python 版本

In short:

Type python in a command prompt

Simply open the command prompt (Win + R) and type cmd and in the command prompt then typing python will give you all necessary information regarding versions:

Python version

梦里兽 2025-01-03 18:06:53

要检查 Jupyter Notebook 中的 Python 版本,您可以使用:

from platform import python_version
print(python_version())

获取版本号,as:

3.7.3

或:

import sys
print(sys.version)

获取更多信息,as

3.7.3 (default, Apr 24 2019, 13:20:13) [MSC v.1915 32 bit (Intel)]

或:

sys.version_info

获取主要版本、次要版本和微版本,as

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

To check the Python version in a Jupyter notebook, you can use:

from platform import python_version
print(python_version())

to get version number, as:

3.7.3

or:

import sys
print(sys.version)

to get more information, as

3.7.3 (default, Apr 24 2019, 13:20:13) [MSC v.1915 32 bit (Intel)]

or:

sys.version_info

to get major, minor and micro versions, as

sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)
甜中书 2025-01-03 18:06:53

我在 Windows 10 上安装了 Python 3.7.0。

这在命令提示符和 Git Bash 中对我有用:

运行 Python 并检查版本:

py

仅检查您拥有的版本:

py --version

py -V    # Make sure it is a capital V

注意:pythonpython --versionpython -VPython, Python --versionPython -V 对我不起作用。

I have Python 3.7.0 on Windows 10.

This is what worked for me in the command prompt and Git Bash:

To run Python and check the version:

py

To only check which version you have:

py --version

or

py -V    # Make sure it is a capital V

Note: python, python --version, python -V,Python, Python --version, Python -V did not work for me.

迷爱 2025-01-03 18:06:53
>>> import sys; print('{0[0]}.{0[1]}'.format(sys.version_info))
3.5

所以从命令行:

python -c "import sys; print('{0[0]}.{0[1]}'.format(sys.version_info))"
>>> import sys; print('{0[0]}.{0[1]}'.format(sys.version_info))
3.5

so from the command line:

python -c "import sys; print('{0[0]}.{0[1]}'.format(sys.version_info))"
纸短情长 2025-01-03 18:06:53

使用

python -V

python --version

注意:请注意,python -V 命令中的“V”是大写 V。python -v(小写的“v”)将以详细模式启动 Python 。

Use

python -V

or

python --version

NOTE: Please note that the "V" in the python -V command is capital V. python -v (small "v") will launch Python in verbose mode.

黎歌 2025-01-03 18:06:53

您可以使用以下命令获取 Python 的版本

python --version

您甚至可以使用 pip freeze 获取安装在 venv 中的任何软件包的版本:

pip freeze | grep "package name"

或者使用 Python 解释器:

In [1]: import django
In [2]: django.VERSION
Out[2]: (1, 6, 1, 'final', 0)

You can get the version of Python by using the following command

python --version

You can even get the version of any package installed in venv using pip freeze as:

pip freeze | grep "package name"

Or using the Python interpreter as:

In [1]: import django
In [2]: django.VERSION
Out[2]: (1, 6, 1, 'final', 0)
场罚期间 2025-01-03 18:06:53

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

py -0p

One-Liners:

❯❯  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')

The default Python version and the paths of all installed versions on Windows:

py -0p

One-Liners:

❯❯  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')
在梵高的星空下 2025-01-03 18:06:53

在装有 Python 3.9.1 的 Windows 10 上,使用命令行:

    py -V

Python 3.9.1

    py --version

Python 3.9.1

    py -VV

Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit 
(AMD64)]

On Windows 10 with Python 3.9.1, using the command line:

    py -V

Python 3.9.1

    py --version

Python 3.9.1

    py -VV

Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit 
(AMD64)]
累赘 2025-01-03 18:06:53

如果您已位于 REPL 窗口中并且没有看到欢迎消息通过版本号,您可以使用 help() 查看主要版本和次要版本:

>>>help()
Welcome to Python 3.6's help utility!
...

If you are already in a REPL window and don't see the welcome message with the version number, you can use help() to see the major and minor version:

>>>help()
Welcome to Python 3.6's help utility!
...
溺ぐ爱和你が 2025-01-03 18:06:53

在 Windows 上的命令提示符中输入 where python 可能会告诉您多个不同版本的 python 的安装位置(假设它们已添加到您的路径中)。

在命令提示符中输入 python -V 应该会显示版本。

Typing where python on Windows into a Command Prompt may tell you where multiple different versions of python are installed, assuming they have been added to your path.

Typing python -V into the Command Prompt should display the version.

转身以后 2025-01-03 18:06:53

如果您安装了 Python,那么检查版本号的最简单方法是在命令提示符中键入“python”。它将显示版本号以及它是在 32 位还是 64 位上运行以及一些其他信息。对于某些应用程序,您希望拥有最新版本,有时则不需要。这取决于您要安装或使用哪些软件包。

If you have Python installed then the easiest way you can check the version number is by typing "python" in your command prompt. It will show you the version number and if it is running on 32 bit or 64 bit and some other information. For some applications you would want to have a latest version and sometimes not. It depends on what packages you want to install or use.

稀香 2025-01-03 18:06:53

对我来说,打开 CMD 并运行

py

将显示类似的内容

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

For me, opening CMD and running

py

will show something like

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.
几度春秋 2025-01-03 18:06:53

要验证 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'

To verify the Python version for commands on Windows, run the following commands in a command prompt and verify the output:

c:\> python -V
Python 2.7.16

c:\> py -2 -V
Python 2.7.16

c:\> py -3 -V
Python 3.7.3

Also, to see the folder configuration for each Python version, run the following commands:

For Python 2, 'py -2 -m site'
For Python 3, 'py -3 -m site'
趁微风不噪 2025-01-03 18:06:53

有两种简单的方法可以检查已安装的 Python 版本。

在命令提示符下运行任何代码:

python -v

python --version

There are two simple ways to check for the version of Python installed.

Run any of the codes on the command prompt:

python -v

or

python --version
柏拉图鍀咏恒 2025-01-03 18:06:53

只需创建一个以 .py 结尾的文件并将以下代码粘贴到并运行它。

#!/usr/bin/python3.6

import platform
import sys

def linux_dist():
  try:
    return platform.linux_distribution()
  except:
    return "N/A"

print("""Python version: %s
dist: %s
linux_distribution: %s
system: %s
machine: %s
platform: %s
uname: %s
version: %s
""" % (
sys.version.split('\n'),
str(platform.dist()),
linux_dist(),
platform.system(),
platform.machine(),
platform.platform(),
platform.uname(),
platform.version(),
))

如果系统上安装了多个Python解释器版本,请运行以下命令。

在 Linux 上,在终端中运行:

ll /usr/bin/python*

在 Windows 上,在命令提示符中运行:

dir %LOCALAPPDATA%\Programs\Python

Just create a file ending with .py and paste the code below into and run it.

#!/usr/bin/python3.6

import platform
import sys

def linux_dist():
  try:
    return platform.linux_distribution()
  except:
    return "N/A"

print("""Python version: %s
dist: %s
linux_distribution: %s
system: %s
machine: %s
platform: %s
uname: %s
version: %s
""" % (
sys.version.split('\n'),
str(platform.dist()),
linux_dist(),
platform.system(),
platform.machine(),
platform.platform(),
platform.uname(),
platform.version(),
))

If several Python interpreter versions are installed on a system, run the following commands.

On Linux, run in a terminal:

ll /usr/bin/python*

On Windows, run in a command prompt:

dir %LOCALAPPDATA%\Programs\Python
娇妻 2025-01-03 18:06:53

在命令提示符中输入以下内容通常会告诉您已安装的 Python 版本:

python -V

但是,如果您未能将 Python 添加到 PATH 变量,则此操作将不起作用。 (如下所示,Python在安装时默认不会添加到系统路径中。)另请参阅:添加 PATH 环境变量Windows 10

另外,请记住您可能安装了多个版本的 Python。我安装了 Python 作为某些东西的依赖项,并安装了我用来编写代码的另一个版本。

Python 安装提示

另一种方法是执行以下操作:

  1. 打开文件资源管理器。
  2. 导航到 C: 驱动器(或您认为可能安装了 Python 的任何驱动器)。
  3. 在右上角的搜索框中,键入 python.exe。这应该可以找到任何 Python 可执行文件。
  4. 在搜索结果中,查看文件路径中的数字(记住任何表明已安装的路径(即,您的下载目录并不一定意味着该版本已安装,但您的程序目录会。) 。

数字对应于版本 src="https://i.sstatic.net/bZn3qH5U.png" alt="python 搜索结果">

例如,这里的许多搜索结果表明我安装了 3.11 版本。如果您想将 Python 添加到您的路径中,并且您可能需要参考此文件,请打开此文件。

Entering the following in the command prompt will typically tell you the version of Python you have installed:

python -V

However, if you failed to add Python to your PATH variable, this won't work. (As shown below, Python isn't added to your system path by default upon installation.) See also: Add PATH environment variable in Windows 10

Also, keep in mind you might have more than one version of Python installed. I have Python installed as a dependency for something and another version installed that I use to write code.

Python installation prompt

An alternative method would be to do the following:

  1. Open File Explorer.
  2. Navigate to the C: drive (or whichever drive where you think you might have installed Python.)
  3. In the search box in the top right corner, type python.exe. This should locate any Python executables.
  4. In your search results, take a look at the numbers in the file paths (keep in mind any paths that suggest it's installed (ie. your downloads directory doesn't necessarily mean that's a version that's installed, but your program directory would.) These numbers correspond to versions.

python search results

For example, many of the search results here indicate that I have version 3.11 installed. Keep these search results open if you want to add Python to your path and you may want to refer to this in order to do so.

唠甜嗑 2025-01-03 18:06:53

对于最新版本,请使用以下 python 版本命令

py -V

For the latest versions please use the below command for the python version

py -V
樱娆 2025-01-03 18:06:53

主要使用命令:

python --version

或者

python -V

Mostly usage commands:

python --version

Or

python -V
爱要勇敢去追 2025-01-03 18:06:53

对于 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

如果您只需要 major.minor 版本(例如 3.6),您可以使用上面的内容,然后选择前 3 个字符:

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

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

For bash scripts this would be the easiest way:

# 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

And if you just need the major.minor version (e.g. 3.6) you can either use the above and then pick the first 3 characters:

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
故事和酒 2025-01-03 18:06:53

打开命令提示符窗口(按 Windows + R,输入 cmd,然后按 Enter)。

输入python.exe

Open a command prompt window (press Windows + R, type in cmd, and hit Enter).

Type python.exe

思念满溢 2025-01-03 18:06:52

要检查 Python 软件版本,应在命令提示符下使用以下代码:

python -V

参考: http://docs.python.org/using/cmdline.html#generic-options

To check the version of one's Python's Software version, one should use the following code in command prompt:

python -V

Reference: http://docs.python.org/using/cmdline.html#generic-options

两仪 2025-01-03 18:06:52

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

import sys
print(sys.version)

In a Python IDE, just copy and paste in the following code and run it (the version will come up in the output area):

import sys
print(sys.version)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文