1、源代码
扩展名:.py,由Python程序解释,不需要编译。
--创建hello.py源文件
1
2
|
# cat hello.py
print
'Hello World!'
|
--执行hello.py
1
2
3
4
5
|
[root@XjTest study]
# chmod a+x hello.py
[root@XjTest study]
# python hello.py
Hello World!
[root@XjTest study]
# ./hello.py
.
/
hello.py: line
1
:
print
: command
not
found
|
备注:./hello.py方式不能执行Python文件,原因:没有指定Python解析器。
1
2
3
4
5
|
[root@XjTest study]
# cat hello.py
#!/usr/bin/python
print
'Hello World!'
[root@XjTest study]
# ./hello.py
Hello World!
|
2、字节代码
扩展名:.pyc,由Python源文件经编译后生成的。
--生成hello.pyc
1
2
3
4
5
6
7
8
9
10
11
|
[root@XjTest study]
# python
Python
2.6
.
6
(r266:
84292
, Sep
4
2013
,
07
:
46
:
00
)
[GCC
4.4
.
7
20120313
(Red Hat
4.4
.
7
-
3
)] on linux2
Type
"help"
,
"copyright"
,
"credits"
or
"license"
for
more information.
>>>
import
py_compile
>>> py_compile.
compile
(
'hello.py'
)
>>> exit()
[root@XjTest study]
# ll
总用量
8
-
rwxr
-
x
-
-
x
1
root root
39
7
月
6
11
:
47
hello.py
-
rw
-
r
-
-
-
-
-
1
root root
117
7
月
6
11
:
50
hello.pyc
|
--执行
1
2
|
[root@XjTest study]
# python hello.pyc
Hello World!
|
3、优化代码
扩展名:.pyo,经过优化的源文件。
--生成hello.pyo
1
2
3
4
5
6
|
# python -O -m py_compile hello.py
[root@XjTest study]
# ll
总用量
12
-
rwxr
-
x
-
-
x
1
root root
39
7
月
6
11
:
47
hello.py
-
rwxr
-
x
-
-
x
1
root root
117
7
月
6
11
:
50
hello.pyc
-
rwxr
-
x
-
-
-
1
root root
117
7
月
6
11
:
56
hello.pyo
|
--执行
1
2
|
[root@XjTest study]
# python hello.pyo
Hello World!
|
1、源代码
扩展名:.py,由Python程序解释,不需要编译。
--创建hello.py源文件
1
2
|
# cat hello.py
print
'Hello World!'
|
--执行hello.py
1
2
3
4
5
|
[root@XjTest study]
# chmod a+x hello.py
[root@XjTest study]
# python hello.py
Hello World!
[root@XjTest study]
# ./hello.py
.
/
hello.py: line
1
:
print
: command
not
found
|
备注:./hello.py方式不能执行Python文件,原因:没有指定Python解析器。
1
2
3
4
5
|
[root@XjTest study]
# cat hello.py
#!/usr/bin/python
print
'Hello World!'
[root@XjTest study]
# ./hello.py
Hello World!
|
2、字节代码
扩展名:.pyc,由Python源文件经编译后生成的。
--生成hello.pyc
1
2
3
4
5
6
7
8
9
10
11
|
[root@XjTest study]
# python
Python
2.6
.
6
(r266:
84292
, Sep
4
2013
,
07
:
46
:
00
)
[GCC
4.4
.
7
20120313
(Red Hat
4.4
.
7
-
3
)] on linux2
Type
"help"
,
"copyright"
,
"credits"
or
"license"
for
more information.
>>>
import
py_compile
>>> py_compile.
compile
(
'hello.py'
)
>>> exit()
[root@XjTest study]
# ll
总用量
8
-
rwxr
-
x
-
-
x
1
root root
39
7
月
6
11
:
47
hello.py
-
rw
-
r
-
-
-
-
-
1
root root
117
7
月
6
11
:
50
hello.pyc
|
--执行
1
2
|
[root@XjTest study]
# python hello.pyc
Hello World!
|
3、优化代码
扩展名:.pyo,经过优化的源文件。
--生成hello.pyo
1
2
3
4
5
6
|
# python -O -m py_compile hello.py
[root@XjTest study]
# ll
总用量
12
-
rwxr
-
x
-
-
x
1
root root
39
7
月
6
11
:
47
hello.py
-
rwxr
-
x
-
-
x
1
root root
117
7
月
6
11
:
50
hello.pyc
-
rwxr
-
x
-
-
-
1
root root
117
7
月
6
11
:
56
hello.pyo
|
--执行
1
2
|
[root@XjTest study]
# python hello.pyo
Hello World!
|
本文转自stock0991 51CTO博客,原文链接:http://blog.51cto.com/qing0991/1434985,如需转载请自行联系原作者