How to make a .pyc file from Python script

MikhilMC picture MikhilMC · Sep 15, 2015 · Viewed 19.6k times · Source

I know that when Python script is imported in other python script, then a .pyc script is created. Is there any other way to create .pyc file by using linux bash terminal?

Answer

User007 picture User007 · Sep 15, 2015

Use the following command:

python -m compileall <your_script.py>

This will create your_script.pyc file in the same directory.

You can pass directory also as :

python -m compileall <directory>

This will create .pyc files for all .py files in the directory

Other way is to create another script as

import py_compile
py_compile.compile("your_script.py")

It also create the your_script.pyc file. You can take file name as command line argument