温馨提示×

ubuntu如何启动python

小樊
45
2026-09-10 09:59:16
栏目: 编程语言

在 Ubuntu 上启动 Python 有多种方式,下面按常见场景给你说明:


一、确认是否已安装 Python

一般 Ubuntu 默认自带 Python 3:

python3 --version

如果显示版本号(如 Python 3.10.12),说明已安装。


二、启动 Python 交互式环境

1. 启动 Python 3

python3

进入后你会看到类似:

Python 3.10.12 (main, ...)
>>>

输入代码即可运行,例如:

print("hello")

退出:

exit()

或按:

Ctrl + D

2. 启动 Python 2(不推荐,已停止维护)

python

新版本 Ubuntu 通常已不再默认安装 Python 2


三、运行 Python 脚本文件

假设有一个文件 test.py

print("hello ubuntu")

运行方式:

python3 test.py

四、使用虚拟环境(推荐)

避免污染系统环境:

python3 -m venv venv
source venv/bin/activate
python

退出虚拟环境:

deactivate

五、没有 Python 怎么办(安装)

sudo apt update
sudo apt install python3

如需 pip:

sudo apt install python3-pip

六、常见问题

  • python 命令不存在?
    → Ubuntu 默认用 python3
  • 想用 python 指向 python3?
    sudo apt install python-is-python3
    

如果你是想:

  • 开机自启 Python 脚本
  • 用 systemd 管理
  • 在服务器/容器里运行

可以告诉我具体需求,我可以给你更详细的方案。

0