I was trying to cythonize my existing project. As it requires to convert all .py file to .pyc. I came across an easy solution to do the same. Just wanted to share this for another seeker like me.
CodePudding user response:
with python installed, you could just do-
python -m compileall .
or
python3 -m compileall .
on your program directory.
CodePudding user response:
The existing solutions on stackoverflow required installation of additional modules. The below solution uses built-in linux module.
find . -depth -name "*.py" -exec sh -c 'f="{}"; mv -- "$f" "${f%.py}.pyx"' \;
This works recursively for all files in the folder.
CodePudding user response:
Python has a compileall
module exactly for this
import compileall
compileall.compile_dir('Lib/', force=True)
# Perform same compilation, excluding files in .svn directories.
import re
compileall.compile_dir('Lib/', rx=re.compile(r'[/\\][.]svn'), force=True)
# pathlib.Path objects can also be used.
import pathlib
compileall.compile_dir(pathlib.Path('Lib/'), force=True)
If you just want to compile 1 file you can use py_compile
import py_compile
py_compile.compile("file.py","output/path")