Linux环境
- 大多Linux发行版均默认安装了Python环境。
- 输入Python可启动Python交互模式
- 程序编辑推荐使用VIM
Windows环境
- 可下载安装Python的msi包直接安装
- 自带Python的GUI开发环境
- 开发工具很多
# Linux交互界面 [root@web1 ~]# python Python 2.6.6 (r266:84292, Jan 22 2014, 09:37:14) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. # 退出指令 >>>exit() [root@web1 ~]# # 文本模式 [root@web1 ~]# vim 1.py print 'hello world' [root@web1 ~]# python 1.py hello world >>> print 'hello world' hello world >>> exit() [root@web1 ~]#
Python文件类型
源代码
- Python源代码的文件以“py”为扩展名,由Python程序解释,不需要编译
[root@web1 ~]# vim 1.py # python标准格式 #!/usr/bin/python print 'hello world' [root@web1 ~]# chmod +x 1.py [root@web1 ~]# ls -l 1.py -rwxr-xr-x 1 root root 39 6月 21 08:59 1.py [root@web1 ~]# ./1.py hello world
字节代码
- Python源文件经编译后生产的扩展名为“pyc”的文件
# 引入模块,对1.py执行编译 [root@web1 shell]# vim 2.py import py_compile py_compile.compile("1.py") # 用python进行编译 [root@web1 shell]# python 2.py [root@web1 shell]# # 这里会生成一个以pyc结尾的文件 [root@web1 shell]# ls -l 总用量 12 -rwxr-xr-x 1 root root 39 6月 21 08:59 1.py -rw-r--r-- 1 root root 112 6月 21 10:37 1.pyc -rwxr-xr-x 1 root root 46 6月 21 10:37 2.py # 这个文件也可以执行 [root@web1 shell]# python 1.pyc hello world
优化代码
- 经过优化的源文件,扩展名为“pyo”
[root@web1 shell]# python -O -m py_compile 1.py [root@web1 shell]# ls -l 总用量 16 -rwxr-xr-x 1 root root 39 6月 21 08:59 1.py -rwxr-xr-x 1 root root 112 6月 21 10:37 1.pyc -rwxr-xr-x 1 root root 112 6月 21 10:44 1.pyo -rwxr-xr-x 1 root root 46 6月 21 10:37 2.py
三种代码执行效果
[root@web1 shell]# python 1.py hello world [root@web1 shell]# python 1.pyc hello world [root@web1 shell]# python 1.pyo hello world
转载于:https://blog.51cto.com/maocong/1664026