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): ... 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 . -O flag, for bytecode (.pyo file iso .pyc) compilation.compileall on my site-packages folder and it messed up everythingIt'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") __pycache__/file.cpython-32.pyc and you get that as the return value.I found several ways to compile python scripts into bytecode
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 . . . Using py_compile.compile:
import py_compile py_compile.compile('YourFileName.py') 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.
Using compileall.compile_dir():
import compileall compileall.compile_dir(direname) It compiles every single Python file present in the supplied directory.
Using compileall.compile_file():
import compileall compileall.compile_file('YourFileName.py') Take a look at the links below:
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.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.
compileall does not include logic to skip files for which the corresponding .pyc file is already up-to-date, does it?compileall does skip files that already have an up-to-date .pyc (tested with Python 2.7.11)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
.pycfiles in the folders as in Python2 by using:
python3 -m compileall -b <pythonic-project-name>The option
-btriggers the output of.pycfiles to their legacy-locations (i.e. the same as in Python2).
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
Let's ask python to compile with this tiny script. Call it compile.py
import py_compile import sys banner=""" ██████ ██ ██ ██████ ██████ ███ ███ ██████ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██ ████ ████ ██ ██ ██ ██ ██ ██████ ████ █████ ██ ██ ██ ██ ████ ██ ██████ ██ ██ █████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██████ ██████ ██ ██ ██ ██ ███████ ███████ """ print(banner) in_file = sys.argv[1]; out_file = (in_file.split('.')[0]) + ".pyc" py_compile.compile(f"{in_file}",f"{out_file}") print(f"{out_file} compiled") When executed in the windows console as python compile.py cheat.py produces a file called cheat.pyc and prints the following in the console. The you can just type cheat.pyc to execute it in the console.
This was made possible with Python 3.9.6 64Bit in Windows 10