Difference between .py and .pyc files.


.py files


.py extension used by the python to save the source code files. This is the script file format used by the python programing language. Python program can be written on any text editor but when we save the code with the extension “.py” the file is saved as the python file. These py files then can be executed on any machine where Python is installed. This file contains:


  • High-level
  • Human readable
  • Python source code
As the .py file enter the interpreter a duplicate file is created with the same name but the different extension and different data format. .py files are slower in execution because the python is an interpreted language.


.pyc files

.pyc extension is used to save the Interpreted bytecode. These type of files are created after the python source code are interpreted. Name of the file is the same as the name of the source code and are ready to run on a virtual machine. These files are faster in generating output as compared to .py files. “.pyc” files are:

  • Faster
  • Machine-readable
  • Low-level
  • Intermediate code
.pyc files are stored in a specific folder and are only created when the python source code is compiled. As these files are already interpreted speed of these files are faster than .py files.


_pycache_ files


All the interpreted files are stored in the _pycache_ files. We can say _pycache_ file is a folder containing all the interpreted .pyc files. The data in .pyc files in the _pycahce _ folder the is the bytecode representation of the python source code. 






Difference between .py and .pyc files?



.py file.pyc file
These files store the source code of the Python programsThese files store the bytecode of the Python programs
Speed of the .py file is lower while generating outputSpeed of the .pyc file is higher than the .py file as it contains the compiled codes.
.py files store the human-readable high-level python code.pyc files store the machine-readable low-level intermediate code
Data in this file is interpreted by the interpreter of pythonThe data in this is the interpreted data which is ready to run on a virtual machine



Python compiles the .py files and saves it as .pyc files , so it can reference them in subsequent invocations. The .pyc contain the compiled bytecode of Python source files. The .pyc contain the compiled bytecode of Python source files, which is what the Python interpreter compiles the source to. This code is then executed by Python's virtual machine . There's no harm in deleting them (.pyc), but they will save compilation time if you're doing lots of processing.Python is an interpreted language , as opposed to a compiled one, though the distinction can be blurry because of the presence of the bytecode compiler. Compiling usually means converting to machine code which is what runs the fastest. But interpreters take human readable text and execute it. They may do this with an intermediate stage .

For example, When you run myprog.py source file, the python interpreter first looks to see if any 'myprog.pyc' (which is the byte-code compiled version of 'myprog.py') exists, and if it is more recent than 'myprog.py'. If so, the interpreter runs it. If it does not exist, or 'myprog.py' is more recent than it (meaning you have changed the source file), the interpreter first compiles 'myprog.py' to 'myprog.pyc'.

There is one exception to the above example. If you put '#! /usr/bin/env python' on the first line of 'myprog.py', make it executable , and then run 'myprog.py' by itself.
Previous Post Next Post