CentOS下Python脚本如何运行

73
2025/2/12 0:31:23
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在 CentOS 系统下运行 Python 脚本,首先确保已经安装了 Python。CentOS 7 默认安装的是 Python 2.x,而 CentOS 8 默认安装的是 Python 3.x。如果你需要使用 Python 3,请确保已经安装了它。以下是在 CentOS 下运行 Python 脚本的步骤:

  1. 打开终端。

  2. 使用文本编辑器(如 vim、nano 等)创建一个 Python 脚本。例如,我们创建一个名为 hello.py 的简单脚本:

vim hello.py
  1. 在编辑器中输入以下内容:

对于 Python 2.x:

#!/usr/bin/env python
print "Hello, World!"

对于 Python 3.x:

#!/usr/bin/env python3
print("Hello, World!")
  1. 保存并关闭文件。

  2. 为脚本添加可执行权限:

chmod +x hello.py
  1. 运行脚本:

对于 Python 2.x:

./hello.py

对于 Python 3.x:

./hello.py

这将输出 “Hello, World!”。

注意:如果你的系统中没有安装 Python 3,你可以通过以下命令安装:

sudo yum install python3

或者,如果你需要安装 Python 2(不推荐,因为 CentOS 7 已经停止支持 Python 2):

sudo yum install python2

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: CentOS中怎么配置和优化PostgreSQL性能