Home > Blockchain >  How do I cythonize a django project?
How do I cythonize a django project?

Time:10-01

Update:

Goals:

  1. To reduce code size.
  2. To reduce latency in the request-response model.
  3. To make it execute fast.

Original question:

How to convert all .py files to .pyx or .pyc? 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")
  • Related