1、运行Python程序

  Python是一种解释型的编程语言,通常Linux系统里默认已经安装了python。

在SHELL提示符下输入:

# which python  --> 输出python的PATH :/usr/bin/python

# python -V  --> 输出python的当前版本:Python 2.6.5(操作系统:Centos6.0)

1.1、 Python交互模式执行
[root@test ~]# python
Python 2.6.5 (r265:79063, Jun 25 2011, 08:36:25)
[GCC 4.4.4 20100726 (Red Hat 4.4.4-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print ('Hello world!')
Hello world!
>>>

[root@test ~]# python
Python 2.6.5 (r265:79063, Jun 25 2011, 08:36:25)
[GCC 4.4.4 20100726 (Red Hat 4.4.4-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Hello world!"
Hello world!
>>> import sys
>>> sys.exit()
退出:"Ctrl+D"或 输入'import sys;sys.exit()'

1.2、Python脚本方式执行

first.py内容:

#!/usr/bin/python

print ("hello world")

# chmod u+x first.py

将当前路径"/root/Public"加入PATH

[root@test Public]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/Public

直接执行first.py
[root@test Public]# first.py
hello world

1.3、源程序编码设置

  Python的源文件可以通过编码使用ASCII以外的字符集,最好的做法是在#!行后面用一种特殊的注释行来定义字符集:

  #  -*- coding: encoding -*-

例:

#!/usr/bin/python
#
-*-coding:utf-8-*-

print ("显示中文")

执行后输出内容:显示中文


 

posted on 2011-08-15 14:30  xunya  阅读(895)  评论(0)    收藏  举报