Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I manually generate a .pyc file from a .py file

Tags:

python

People also ask

How is .PYC created?

pyc files are created automatically by the GraalVM Python runtime when no or an invalid . pyc file is found matching the desired . py file. When a Python source file (module) is imported during an execution for the first time, the appropriate .

What is the difference between .py and .PYC files?

py files contain the source code of a program. Whereas, . pyc file contains the bytecode of your program.

When you save a Python program it creates .PYC What is it?

pyc files are created by the Python interpreter when a . py file is imported. They contain the "compiled bytecode" of the imported module/program so that the "translation" from source code to bytecode (which only needs to be done once) can be skipped on subsequent imports if the . pyc is newer than the corresponding .


You can use compileall in the terminal. The following command will go recursively into sub directories and make pyc files for all the python files it finds. The compileall module is part of the python standard library, so you don't need to install anything extra to use it. This works exactly the same way for python2 and python3.

python -m compileall .

You can compile individual files(s) from the command line with:

python -m compileall <file_1>.py <file_n>.py

It's been a while since I last used Python, but I believe you can use py_compile:

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

I found several ways to compile python scripts into bytecode

  1. Using py_compile in terminal:

    python -m py_compile File1.py File2.py File3.py ...
    

    -m specifies the module(s) name to be compiled.

    Or, for interactive compilation of files

    python -m py_compile -
    File1.py
    File2.py
    File3.py
       .
       .
       .
    
  2. Using py_compile.compile:

    import py_compile
    py_compile.compile('YourFileName.py')
    
  3. Using py_compile.main():

    It compiles several files at a time.

    import py_compile
    py_compile.main(['File1.py','File2.py','File3.py'])
    

    The list can grow as long as you wish. Alternatively, you can obviously pass a list of files in main or even file names in command line args.

    Or, if you pass ['-'] in main then it can compile files interactively.

  4. Using compileall.compile_dir():

    import compileall
    compileall.compile_dir(direname)
    

    It compiles every single Python file present in the supplied directory.

  5. Using compileall.compile_file():

    import compileall
    compileall.compile_file('YourFileName.py')
    

Take a look at the links below:

https://docs.python.org/3/library/py_compile.html

https://docs.python.org/3/library/compileall.html


I would use compileall. It works nicely both from scripts and from the command line. It's a bit higher level module/tool than the already mentioned py_compile that it also uses internally.


In python2 you should use:

python -m compileall <pythonic-project-name>

compiles all .py files to .pyc files in a project which contains packages as well as modules.


In python3 you should use:

python3 -m compileall <pythonic-project-name>

compiles all .py files to __pycache__ folders in a project which contains packages as well as modules.

Or with browning from this post:

You can enforce the same layout of .pyc files in the folders as in Python2 by using:

python3 -m compileall -b <pythonic-project-name>

The option -b triggers the output of .pyc files to their legacy-locations (i.e. the same as in Python2).