Cython - Hiding the Code of a Django Project

 For this post I will be using this Django project:

git clone https://github.com/38130/mysite

 

 

Installation 

pip install cython

 

 Generate the Compiled Code

from setuptools import setup, Extension
from Cython.Build import cythonize

exts = [
    Extension("mysite.mysite.settings", ["mysite/mysite/settings.py"]),
    Extension("mysite.mysite.urls", ["mysite/mysite/urls.py"]),
    Extension("mysite.mysite.asgi", ["mysite/mysite/asgi.py"]),
    Extension("mysite.mysite.wsgi", ["mysite/mysite/wsgi.py"]),
    Extension("mysite.polls.models", ["mysite/polls/models.py"]),
    Extension("mysite.polls.views", ["mysite/polls/views.py"]),
    Extension("mysite.polls.admin", ["mysite/polls/admin.py"]),
    Extension("mysite.polls.urls", ["mysite/polls/urls.py"]),
    Extension("mysite.polls.apps", ["mysite/polls/apps.py"]),

]
setup(
    ext_modules=cythonize(
        exts,
        build_dir="build_cython",
    ),
)

 

python setup.py build_ext --inplace

May require updated Visual Studio Build Tools https://visualstudio.microsoft.com/visual-cpp-build-tools/

  • Desktop development with C++
  • MSVC v14.x C++ build tools
  • Windows 10 SDK (or 11 SDK)

 

You can now remove all the .py files:

  • mysite/mysite/settings.py
  • mysite/mysite/urls.py
  • mysite/mysite/asgi.py
  • mysite/mysite/wsgi.py
  • mysite/polls/models.py
  • mysite/polls/views.py
  • mysite/polls/admin.py
  • mysite/polls/urls.py
  • mysite/polls/apps.py

 

python manage.py runserver

Tested with:
Cython==3.1.4
Django 5.2
Windows



15 Oct. 2025 | Last Updated: 03 Dec. 2025 | jaimedcsilva

Related
  • Using ngrok with Django
  • Opening a Django project through a .exe file
  • Creating an online store with Django
  • CRUD
  • Creating a Basic Django Project Automatically
  • Filter Horizontal in a custom template
  • GeoIP tracking with IpInfo and Django
  • GeoIP tracking with MaxMind and Django
  • Django User Agents
  • Generating Temporary Download Links
  • Cython - Hiding the Code of a Django Project
  • Quick & Easy Django Deployment on PythonAnywhere
  • A Brief History of Django
  • Django & Paypal Webhooks
  • Generating QR Codes with Django

  • Buy Me a Coffee