Learning notes for python file types

Source: Internet
Author: User

I. File classification:

1) *. py file, which is a common py source file;
2) * The binary bytecode file compiled from the pyc and py source files is still loaded and executed by python, but the speed will increase and the source code will be hidden;
3) *. pyo: optimizes the compiled program. It is also a binary file and is suitable for embedded systems.

II. Generate pyc and pyo files

1) how to generate a pyc file?

Suppose we have a 1. py file that needs to be compiled into a pyc file, then input
Import py_compile
Py_compile.compile ('1. Py ')
In this way, the pyc file can be generated.
2) how to generate a pyo file?
Python-O-m py_compile 1.py

In addition, the pyc and pyo files are the same as the py files, and can still be executed in the form of python 1. pyc.

The python source code file uses py as the extension, which is explained by the python program and does not need to be compiled. The following code is hello. py.

[Root @ AY130704092906278009Z python] # cat hello. py
#! /Usr/bin/python
Print ("hello world ")
 

Byte Code

Python source file generated after compilation with the extension pyc

Write a python program to compile the above hello. py code: (2. The py program code is as follows)


[Root @ AY130704092906278009Z python] # cat 2.py
Import py_compile
 
Py_compile.compile ('Hello. Py ')

Run python 2. py and you can see that a _ pycache _ folder is generated. The following is a pyc file, which can be directly executed.


[Root @ AY130704092906278009Z python] # python 2.py
[Root @ AY130704092906278009Z python] # ls
2. py hello. py reference. py str_methods.py using_list.py
Code _ pycache _ seq. py using_dict.py using_tuple.py
Code Optimization

Optimized source file named. pyo
Run:

Python-O-m py_compile hello. py, which is also generated in the _ pycache _ folder.


[Root @ AY130704092906278009Z python] # cd _ pycache __/
[Root @ AY130704092906278009Z _ pycache _] # ls
Hello cpython-33.pyc hello. Cpython-33.pyo
[Root @ AY130704092906278009Z _ pycache _] # python hello. Cpython-33.pyc
Hello world
[Root @ AY130704092906278009Z _ pycache _] # python hello. Cpython-33.pyo
Hello world


The above three file formats are python. [The above are based on the running results on python3.3. Other python versions may be different]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.