This page may be out of date. Submit any pending changes before refreshing this page.
Hide this message.
Quora uses cookies to improve your experience. Read more
4 Answers
Avi Mehenwal
Avi Mehenwal, Python programmer | Tester and Automator

Python is a dynamic language, and running it from the command line essentially triggers the following steps:

  • The source is compiled the first time it is encountered (e.g., imported as a module or directly executed). This step generates the binary file, with a .pyc or .pyo extension depending on your system.
  • The interpreter reads the binary file and executes the instructions (opcodes) one at a time.

The python interpreter is stack-based and once source is compiled corresponding .pyc are ready in the same directory (in case of python 2.x). With python 3.x a new __pycache__ directory is created for the source and all compiled Bytecode files are placed underneath.

If you want to manually compile a module or generate its compiled bytecode you can consider using py_compile module 32.10. py_compile - Compile Python source files - Python 2.7.11 documentation.

SAMPLE:
>>> import py_compile
>>> py_compile.compile('http://abc.py')

Once source is compiled you might be further interested in inspecting bytecode. For this purpose cpython proved a module called dis 32.12. dis - Disassembler for Python bytecode - Python 2.7.11 documentation (python disassembler) which comes quiet handy while trying to understand bytecode.

A SAMPLE:

>>> def foo():
...     a = 2
...     b = 3
...     return a + b...

>>> foo.func_code
<code object foo at 0x106353530, file "<stdin>", line 1
>>>> foo.func_code.co_code
'd\x01\x00}\x00\x00d\x02\x00}\x01\x00|\x00\x00|\x01\x00\x17S'
>>> print [ord(x) for x in foo.func_code.co_code]
[100, 1, 0, 125, 0, 0, 100, 2, 0, 125, 1, 0, 124, 0, 0, 124, 1, 0, 23, 83]
>>>
>>>
>>> import dis
>>> dis.dis(foo)
  2           0 LOAD_CONST               1 (2)
              3 STORE_FAST               0 (a)

  3           6 LOAD_CONST               2 (3)
              9 STORE_FAST               1 (b)

  4          12 LOAD_FAST                0 (a)
             15 LOAD_FAST                1 (b)
             18 BINARY_ADD
             19 RETURN_VALUE

I hope it helps !

Your feedback is private.
Is this answer still relevant and up to date?
Ashish Barnwal
Ashish Barnwal, C C++ Java Python Javascript

Python byte code is the code which is generated after compiling a python program. Lets try to understand, suppose you have written a python program and saved it in a file 'MyProgram.py'. This 'MyProgram.py' is source code. Now if you compile this file you would get a file with '.pyc' extention which is called byte code.
I hope It helped.

Your feedback is private.
Is this answer still relevant and up to date?
Boris Krasnoiarov
Boris Krasnoiarov, Director, Enterprise Data at Zendesk (2018-present)

Python byte code is an intermediate language used by CPython (One, most popular atm, implementation of the Python language) to speed-up code execution & load (.pyc & .pyo files are more compact than original source)

While arguably it makes code execution faster, exposing bytecode outside of the interpreter was/is somewhat counterproductive imho - countless developers got in trouble trying to “optimize” python code by precompiling with optimizations. (One somewhat famous side-effect of such optimization is that objects lose their __doc__ attributes - sometimes a nasty surprise to uninitiated developers)

Additional info on different Python implementations: PythonImplementations - Python Wiki (note that they all use different bytecodes (CLI/.NET; Java; …)

Ashish Siska
Ashish Siska, Travel , Adventure, Student , Engineering , MNNIT \U0001f600

Python code goes through 2 stages. First step compiles the code into .pyc files which is actually a bytecode. Then this .pyc file(bytecode) is interpreted using CPython interpreter. Please refer to this link. Here process of code compilation and execution is explained in easy terms.

source

Python Bytecode: Fun with dis