文章目录

In this post, I will give you an example to demonstrates how to generate a .pyc from compiling a python program.

What is the .pyc? A .pyc file is a compiled python bytecode file, you can visit how do I create a pyc file for more information.

Fist, I write a python script named hello.py as following:

1
2
3
4
5
6
7
#!/usr/bin/python
#
# File: hello.py
# Date: June 16th, 2011
# Author: mutse <yyhoo2.young@gmail.com>
#
print “Hello, welcome to python world!”

Then use chmod command to change its file privilege.

1
$ chmod a+x hello.py

Now, edit a python script as pycoder.py.

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/python
#
# File: pycoder.py
# Date: June 16th, 2011
# Author: mutse <yyhoo2.young@gmail.com>
#
import py_compile
file = raw_input("Enter Your File Name: ")
py_compile.compile(file)
print ".pyc of the python script " + file + " is generated."

Then, do the same thing as hello.py.

1
$ chmod a+x pycoder.py

At last, please run pycoder.py script, and a pyc bytecode hello.pyc is generated.

1
$ ./pycoder.py
Enter Your File Name: hello.py
.pyc of the python script hello.py is generated.
文章目录