How can I manually generate a .pyc file from a .py file
Solution 1
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 .
Solution 2
You can compile individual files(s) from the command line with:
python -m compileall <file_1>.py <file_n>.py
Solution 3
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")
Solution 4
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:
https://docs.python.org/3/library/py_compile.html
https://docs.python.org/3/library/compileall.html
Solution 5
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.
Related videos on Youtube
Comments
-
zJay over 2 years
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): ...
-
rvighne over 9 yearsYou 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. -
swdev over 9 yearsThis should be the accepted answer --at least in my need to compile all *.py into *.pyc: recursively :)
-
Kyle Strand over 9 years
compileall
does not include logic to skip files for which the corresponding.pyc
file is already up-to-date, does it? -
Eponymous over 8 years@KyleStrand
compileall
does skip files that already have an up-to-date.pyc
(tested with Python 2.7.11) -
Kyle Strand over 8 years@Eponymous Interesting. Not sure why I thought otherwise. Thanks for checking.
-
D.Snap about 8 yearsIs 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?
-
Devy about 7 yearsOne small correction is that the module name you are loading is
py_compile
andcompileall
NOTpy_compile.py
orcompileall.py
. In other words, it should bepython3 -m py_compile PYTHON_FILENAME
orpython3 -m compileall PYTHON_FILES_DIRECTORY
. -
Melroy van den Berg about 7 yearsI 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. -
Sajuuk almost 6 yearswhat about decompiling?
-
Alex almost 6 yearsbe careful with this command. I did accidentally do a
compileall
on my site-packages folder and it messed up everything -
ColinMac over 5 yearsI'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?
-
hru_d almost 5 yearsI am working on Amazon AMI where
python3 -m compileall .
was just listing all the directories. I had to force it to compile bypython3 -m compileall . -f
-
Kenny over 4 yearsOn 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
-
MacGyver almost 2 years@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.