174

For some reason, I can not depend on Python's "import" statement to generate .pyc file automatically

Is there a way to implement a function as following?

def py_to_pyc(py_filepath, pyc_filepath):
    ...
Serenity
  • 35,289
  • 20
  • 120
  • 115
zJay
  • 2,889
  • 3
  • 22
  • 19

9 Answers9

320

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 .
Hossein
  • 24,202
  • 35
  • 119
  • 224
Marwan Alsabbagh
  • 25,364
  • 9
  • 55
  • 65
  • 6
    This should be the accepted answer --at least in my need to compile all *.py into *.pyc: recursively :) – swdev Sep 12 '14 at 14:58
  • 2
    Is it possible to distribute a PYC file containing all the libraries used? So users doesn't have to install them, just ran the PYC file, I know in java this is possible using a JARs are there any similar method for Python? – D.Snap Mar 15 '16 at 07:22
  • I heard also something about meta files in Python. This compileall also build some cache? If not, what is the command for that?? Since the end-users doesn't have write permission to the lib directory. And I want to speed up things here... PS. also take a look at the `-O` flag, for bytecode (.pyo file iso .pyc) compilation. – Melroy van den Berg Feb 15 '17 at 11:54
  • what about decompiling? – Sajuuk Apr 27 '18 at 11:46
  • 3
    be careful with this command. I did accidentally do a `compileall` on my site-packages folder and it messed up everything – Alex Jul 18 '18 at 11:29
  • I'm using an Anaconda distribution of Python. It's housed in a ProgramData subfolder, where I don't want to store my .py files. How can I use this command to compile a specific folder? – ColinMac Aug 02 '18 at 03:33
  • I am working on Amazon AMI where `python3 -m compileall .` was just listing all the directories. I had to force it to compile by `python3 -m compileall . -f` – hru_d May 21 '19 at 20:14
  • On shell, how would you specify the output file location ? I tried **python -m compileall test.py out_test.pyc** but that would fail. I could not find a solution online – Kenny Sep 09 '19 at 18:52
  • @D.Snap, the most standard way on Linux (non-Debian distributions) is to build a custom RPM. For Python, you can either make an RPM by having your SRPM the py files and the RPM ends up being the pyc. Unless you have made a source distribution or binary distribution in Python. Then the process is a little different. – JustBeingHelpful May 24 '22 at 17:43
  • Recursively: find /py_files_folders -name "*.py" -exec python -m compileall {} \; – FabricioFCarv Aug 04 '22 at 12:09
75

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

python -m compileall <file_1>.py <file_n>.py
derrend
  • 4,176
  • 4
  • 27
  • 40
66

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")
rvighne
  • 20,755
  • 11
  • 51
  • 73
Mike Bailey
  • 12,479
  • 14
  • 66
  • 123
  • 13
    You probably want to include the second parameter, which determines the output file. Otherwise, it defaults to something like `__pycache__/file.cpython-32.pyc` and you get that as the return value. – rvighne Aug 19 '14 at 21:10
61

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

Abhishek Kashyap
  • 3,332
  • 2
  • 18
  • 20
  • 1
    One small correction is that the module name you are loading is `py_compile` and `compileall` NOT `py_compile.py` or `compileall.py`. In other words, it should be `python3 -m py_compile PYTHON_FILENAME` or `python3 -m compileall PYTHON_FILES_DIRECTORY`. – Devy Jan 26 '17 at 21:08
19

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.

Pekka Klärck
  • 2,348
  • 2
  • 18
  • 19
17

Normally the following command compilies a python project:

python -m compileall <project-name>

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

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

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).

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
4

To match the original question requirements (source path and destination path) the code should be like that:

import py_compile
py_compile.compile(py_filepath, pyc_filepath)

If the input code has errors then the py_compile.PyCompileError exception is raised.

ababak
  • 1,685
  • 1
  • 11
  • 23
1

There are two ways to do this

  1. Command line
  2. Using python program

If you are using command line, use python -m compileall <argument> to compile python code to python binary code. Ex: python -m compileall -x ./*

Or, You can use this code to compile your library into byte-code:

import compileall
import os

lib_path = "your_lib_path"
build_path = "your-dest_path"

compileall.compile_dir(lib_path, force=True, legacy=True)

def moveToNewLocation(cu_path):
    for file in os.listdir(cu_path):
        if os.path.isdir(os.path.join(cu_path, file)):
            compile(os.path.join(cu_path, file))
        elif file.endswith(".pyc"):
            dest = os.path.join(build_path, cu_path ,file)
            os.makedirs(os.path.dirname(dest), exist_ok=True)
            os.rename(os.path.join(cu_path, file), dest)

moveToNewLocation(lib_path)

look at ☞ docs.python.org for detailed documentation

2NinerRomeo
  • 2,687
  • 4
  • 29
  • 35
Prashant
  • 394
  • 1
  • 6
  • 18
1
  1. create a new python file in the directory of the file.
  2. type import (the name of the file without the extension)
  3. run the file
  4. open the directory, then find the pycache folder
  5. inside should be your .pyc file
Wudfulstan
  • 129
  • 11