How to Compile Code to Convert py to pyc in Python?

How to Compile Code to Convert py to pyc in Python?

Do you want to convert py to pyc in Python?

Let’s get some background understanding…

What is pyc file in Python?

Whenever you run the Python code, there are two major steps involved.

  • The code (from .py file) gets compiled into byte code and save it as a .pyc file.
  • Byte code from this file gets interpreted.

Python is called as a Byte code interpreted programming language.

This complete process happens in the background so that you don’t see the .pyc file.

This is the usual case when you simply execute any Python program.

py vs pyc file in Python:

If you open any py file, you can see the Python code which is human readable. Whereas, pyc file contains byte code. You can not read it simply.

Now, our interest is to convert the py file into pyc file. For this, you have to compile Python py file without executing it.

When does .pyc file get created?

We import modules in our program. If you are importing module first time, Python compiles the module code and create .pyc file. It is nothing but the bytecode file.

In another scenario, if there is any change in code, python compiles the code and save it as bytecode (.pyc file) before running a python script.

The .pyc file is created in the same folder where the .py file exists.

These are all usual ways, Python compiles and generates the .pyc file.

Why does compiling .py to .pyc not happen?

In certain scenarios, Python does not generate the .pyc file.

This may occur in case you don’t have write permissions for the directory where you are compiling the code. It can also happen when you don’t have enough space to store bytecode (.pyc file).

In this case, if you are running Python code, it may run existing (old) .pyc bytecode. This is the worst behavior to trace.

I still remember when I faced this issue. I was updating my code in a python script, but when I run the code, I was amazed not to see any changes in output even after doing changes in the Python script.

And here is a way to get rid of it.

Convert py to pyc in Python  by Compiling without Executing

So let’s see how to create a .pyc file just with a simple command.

For this, you can use two modules for compiling Python script- py_compile and compileall modules.

Method 1: using py_compile module

Import py_compile in your python code. There is a function called compile(). Pass the Python file name that you want to compile as an argument. It will convert Python script into the bytecode (file with the file extension pyc).

import py_compile
py_compile.compile('abc.py')

This will compile the abc.py file and saved compiled bytecode in abc.pyc file. Python stores bytecode in the same directory where the abc.py file exists.

Method 2: using compile all module

Sometimes you may need to compile all the python script within a given directory. Then this method is good rather than compiling each file independently.

In that case, run the following command in a given directory.

python -m compileall

It will compile all the files in the current directory.

If you are not providing any of the directories in command, it compiles all the python script found in sys.path.

Note, it will just compile Python py file without executing.

In this post, I have not covered anything related to compiler and interpreter as it is not the scope of this article. If you are interested, you can read the difference between Compiler and Interpreter.

This is all about how to convert py to pyc in Python. If you have any doubt, write in the comment.

Happy Pythoning!

9 Comments

  1. Hi Aniruddha,
    Thank you for posting this.
    I needed to know how to compile a library.
    I used method 1.
    could NOT have been easier!

  2. Hi Aniruddha,
    This post is very helpful and exactly what I was looking for. Can you please help me with below questions?
    1. How to execute .pyc file?
    2. Is it possible for anyone to convert .pyc to .py and read/modify the source code?

    1. Hi Swapnil,

      Thanks for your kind words.

      1. You can execute .pyc file through Python interpreter. Simple command: python example.pyc

      2. There are some techniques for doing this. But it is complex and does not give 100% accuracy. Not reliable.

    2. Hi Swapnali,
      Yes, it is possible.
      Use Python decompiler for convert .pyc to .py.
      One of the best python decompiler: “Easy Python Decompiler”.
      You can download it from here.

  3. Hi Aniruddha,

    I have to apply bytecode to my existing python project files. So I have been investigating on Python pyarmor package and converting .py file to .pyc format.

    My question is if i have exisitng whole pyhton project how can I make them not available to end user readable format using these methods or how i can apply to exisitng python project files.

    Thank you in advance!!

    1. Hi Damini,

      I’m not sure, if there is way to convert entire project files to bytecode format. This is still scope for me to explore.

      Rather, I would suggest, you can write a script that read individual files in your project and convert them to .pyc file.

Leave a Reply

Your email address will not be published. Required fields are marked *