Understanding the use of Python bytecode files in different scenarios.

ly vu
3 min readMay 20, 2020

--

Source: http://jikeme.com/demystifying-pyc-files

Python generates a bytecode from the source code of a module when it is first imported to improve the performance of loading a module. There are some scenarios in which Python will make a decision on using an existed bytecode file (.pyc) or recompile the source code file (.py) [1, 2]. In this post, I will experiment to understand better.

Let’s say you develop a module called “helloworld.py” containing your first greeting to the world.

At this moment, you have only a single file in your project.

you then make it available for others to use, they may import your module

  1. Importing the “helloworld” module

You will notice that Python generates a new folder called “__pycache__”

Let’s figure out what is inside this folder: a .pyc file, a python bytecode file.

The file is in binary format; therefore, it is not very easy to examine, as shown in the Figure below.

2. What if we accidentally delete the helloworld.py, and hope that we can still import the module helloworld using the bytecode.

Delete helloworld.py module

But we still keep the bytecode file remained and unchanged.

We observe that although the bytecode still exists, we cannot import the module helloworld as we expected..

3. What if we restore the module helloworld but make changes to it. As an example, I add a new line to the module helloworld.

Adding a line to the module helloworld

AS we import the helloworld module, we see two lines of greetings instead of one.

And we notice that the bytecode has been changed (updated)

4. What if we run the module directly? You can only obtain the bytecode file when you import a module; other cases such as running the script directly does not result in a bytecode file. In the Figure below, you can see that there is “__pycache__” directory generated after I run the command “python helloworld.py.”

5. Takeaways

  • Python somehow checks a python file and its counterpart bytecode file to decide on loading the module
  • If a module is deleted, Python will not load its existing unchanged bytecode but throws an error of missing the source file
  • If a module is updated while its bytecode remain outdated, Python will recompile the module and replace the bytecode with a new one

References

[1]. https://stackoverflow.com/questions/2998215/if-python-is-interpreted-what-are-pyc-files

[2]. http://net-informations.com/python/iq/pyc.htm

--

--