

























本章目标:安装 Python 环境、安装文本编辑器、运行第一个 Python 程序。
Python 自带的终端解释器,可以不用保存完整程序,直接试运行代码片段。
>>> print("Hello Python interpreter!")
Hello Python interpreter!
>>> 是 Python 提示符,表示可以在后面输入 Python 命令>>> 的表示来自终端会话只需一行代码:
print("Hello world!")
如果这行代码能正确运行,你编写的任何 Python 程序都将如此。
Python 是跨平台语言,所有主流操作系统都能运行。
1. 检查 Python 版本
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:38)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
退出 Python:Ctrl + D 或 exit()
检查 Python 3:
$ python3
Python 3.5.0 (default, Sep 17 2015, 13:05:18)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
2. 安装文本编辑器(Geany)
$ sudo apt-get install geany
3. 配置 Geany 使用 Python 3
菜单 Build → Set Build Commands,修改:
python3 -m py_compile "%f"python3 "%f"4. 编写并运行 Hello World
创建文件夹 python_work,文件 hello_world.py:
print("Hello Python world!")
运行:菜单 Build → Execute、单击 Execute 图标或按 F5
输出:
Hello Python world!
------------------
(program exited with code: 0)
Press return to continue
5. 在终端会话中运行 Python 代码
>>> print("Hello Python interpreter!")
Hello Python interpreter!
>>>
1. 检查 Python 版本
$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits", or "license" for more information.
>>>
2. 安装文本编辑器(Sublime Text)
3. 配置 Sublime Text 使用 Python 3
查看 Python 3 路径:
$ type -a python3
python3 is /usr/local/bin/python3
菜单 Tools → Build System → New Build System,输入:
{
"cmd": ["/usr/local/bin/python3", "-u", "$file"],
}
保存为 Python3.sublime-build
4. 运行 Hello World
print("Hello Python world!")
运行:Tools → Build 或 Command + B
输出:
Hello Python world!
[Finished in 0.1s]
1. 安装 Python
2. 启动 Python 终端会话
C:\> python
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 22:15:05) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
如果提示 'python' is not recognized,需手动指定路径:
C:\> C:\Python35\python
3. 安装文本编辑器(Geany)
下载地址:http://geany.org/
4. 配置 Geany
菜单 Build → Set Build Commands:
C:\Python35\python -m py_compile "%f"C:\Python35\python "%f"5. 运行 Hello World
print("Hello Python world!")
运行:菜单 Build → Execute、单击 Execute 图标或按 F5
如果程序无法运行,尝试以下方案:
~$ cd Desktop/python_work/
~/Desktop/python_work$ ls
hello_world.py
~/Desktop/python_work$ python hello_world.py
Hello Python world!
关键命令:
cd — 切换目录(change directory)ls — 列出文件(list)python hello_world.py — 运行 Python 程序C:\> cd Desktop\python_work
C:\Desktop\python_work> dir
hello_world.py
C:\Desktop\python_work> python hello_world.py
Hello Python world!
关键命令:
cd — 切换目录dir — 列出文件(directory)python hello_world.py — 运行 Python 程序如果未配置 PATH,需指定完整路径:
C:\Desktop\python_work> C:\Python35\python hello_world.py
Hello Python world!
浏览 Python 主页( http://python.org/ ),寻找感兴趣的主题。
在 hello_world.py 中故意添加一个输入错误(如将 print 拼写为 PRINT),运行观察错误信息。试找一个不会导致错误的输入错误。
如果你编程技艺无穷,你打算开发什么样的程序?花点时间描绘三个你想创建的程序。
# Hello World — 第一个 Python 程序
print("Hello world!")
# 终端会话中运行 Python 代码片段
>>> print("Hello Python interpreter!")
Hello Python interpreter!
# hello_world.py — 教材实际使用的程序
print("Hello Python world!")
| 错误类型 | 说明 | 解决方法 |
|---|---|---|
NameError |
变量名拼写错误或未定义 | 检查变量名拼写 |
SyntaxError |
语法错误,如 print 首字母大写 |
Python 区分大小写,print 必须全小写 |
| 命令找不到 | PATH 未配置(Windows) | 手动指定 Python 完整路径 |
| 编辑器无法运行 | 未配置正确的 Python 版本 | 设置编译/执行命令为 python3 |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。