Okay, here's a minimal setup.py script to compile Python files using mypyc.
Choose one of the following options based on your project structure.
Option 1: Compiling specific .py files
Use this if your application consists of one or more individual Python files in the root directory (or specific paths).
python
# setup.py
from setuptools import setup
from mypyc.build import mypycify
setup(
name="my_compiled_app", # Replace with your app's name
version="0.1.0", # Replace with your app's version
# List the Python files you want to compile
ext_modules=mypycify([
'app.py', # Your main application file
'utils.py', # Another file to compile
# Add other .py files here
]),
)
Option 2: Compiling an entire package
Use this if your application is structured as a Python package (a directory with an __init__.py file).
python
# setup.py
from setuptools import setup
from mypyc.build import mypycify
setup(
name="my_compiled_app", # Replace with your app's name
version="0.1.0", # Replace with your app's version
# Specify the package(s) to compile. Mypyc will find modules within.
ext_modules=mypycify([
'myapp', # The name of your package directory
# Add other packages or modules if needed
]),
)
(Assumes you have a directory structure like myapp/__init__.py, myapp/main.py, etc.)
How to Use:
Save: Save the chosen script as setup.py in the root directory of your project.
Install mypy: If you haven't already, install mypy (which includes mypyc):
bash
pip install -U mypy
Compile: Run the following command in your terminal from the same directory as setup.py:
bash
python setup.py build_ext --inplace
build_ext: Tells setuptools to build C extensions.
--inplace: Puts the compiled shared object files (.so on Linux/macOS, .pyd on Windows) directly next to your Python source files, allowing you to run your application immediately using the compiled versions.
Now, when you run your Python application (e.g., python app.py or python -m myapp), Python will automatically import the faster, compiled versions where available.