在 Python 2 和 3 之间切换
Python 在不同的操作系统环境(Windows、macOS、Linux 和 Android)中有不同的版本。 自 1991 年以来,它已经有了三个主要版本(1、2 和 3)。
Python 3.x 在使用上比 2.x 和 1.x 更可取,这是正确的。 但是,包括您在内的某些人可能希望使用 Python 2.x 访问某些遗留代码或处理某些事情。
本文将展示如何在 Python 2 和 3 之间切换以运行 Python 代码。
在 Python 2 和 3 之间切换
对于不同的操作系统,开关会有所不同; Linux 和 macOS 的工作原理完全相同,但 Windows 却大不相同。 假设您的 PC 上安装了这两个版本。
在 Linux 和 macOS 中切换 Python 2 和 3
您可以使用以下适用于 Linux 和 macOS 的命令切换到 Python 2.x。
python2
命令的输出:
Python 2.7.18 (default, Jul 1 2022, 12:27:04)
[GCC 9.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
如果您需要运行 Python 2 文件:
python2 version.py
对于 Python 3,我们可以使用以下命令:
python
命令的输出:
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
并运行 Python 3 文件:
python version.py
在 Windows 中切换 Python 2 和 3
对于 Windows,您可以使用以下命令切换到 Python 2.x。
py -2
命令的输出:
Python 2.6.2 (r262:71605, Apr 14 2009, 22:46:50) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
如果需要执行 Python 2 文件:
py -2 index.py
要切换到 Python 3.x 并使用其解释器,您可以使用以下命令:
py -3
命令的输出:
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串