0
# MypyC Compiler
1
2
Python-to-C compiler that generates efficient C extensions from Python code with type annotations. MypyC can significantly improve runtime performance for type-annotated Python code.
3
4
## Capabilities
5
6
### Compiler Entry Points
7
8
```python { .api }
9
def main() -> None:
10
"""Main compiler entry point (mypyc.__main__.main)."""
11
12
def mypycify(
13
paths: list[str],
14
*,
15
only_compile_paths: Iterable[str] | None = None,
16
verbose: bool = False,
17
opt_level: str = "3",
18
debug_level: str = "1",
19
strip_asserts: bool = False,
20
multi_file: bool = False,
21
separate: bool | list[tuple[list[str], str | None]] = False,
22
skip_cgen_input: Any | None = None,
23
target_dir: str | None = None,
24
include_runtime_files: bool | None = None,
25
strict_dunder_typing: bool = False,
26
group_name: str | None = None,
27
) -> list[Extension]:
28
"""
29
Main entry point to building using mypyc. Compile Python modules to C extensions.
30
31
Parameters:
32
- paths: list[str] - File paths to build (may also contain mypy options)
33
- only_compile_paths: Iterable[str] | None - Only compile these specific paths
34
- verbose: bool - Enable verbose output (default: False)
35
- opt_level: str - Optimization level "0"-"3" (default: "3")
36
- debug_level: str - Debug information level "0"-"3" (default: "1")
37
- strip_asserts: bool - Strip assert statements (default: False)
38
- multi_file: bool - Generate multiple C files (default: False)
39
- separate: bool | list - Compile modules separately (default: False)
40
- skip_cgen_input: Any | None - Skip code generation input (advanced)
41
- target_dir: str | None - Target directory for output files
42
- include_runtime_files: bool | None - Include runtime support files
43
- strict_dunder_typing: bool - Strict typing for dunder methods (default: False)
44
- group_name: str | None - Group name for extensions
45
46
Returns:
47
- list[Extension]: Setuptools Extension objects for compiled modules
48
"""
49
```
50
51
#### Usage Examples
52
53
```bash
54
# Compile a module
55
mypyc mymodule.py
56
57
# Compile with optimizations
58
mypyc --opt-level 3 mymodule.py
59
60
# Multi-file compilation
61
mypyc --multi-file package/
62
```
63
64
### Integration with Build Systems
65
66
```python
67
# Basic setup.py integration
68
from mypyc.build import mypycify
69
from setuptools import setup
70
71
ext_modules = mypycify([
72
"mypackage/core.py",
73
"mypackage/utils.py"
74
], opt_level="3")
75
76
setup(
77
name="mypackage",
78
ext_modules=ext_modules
79
)
80
81
# Advanced setup.py with full options
82
ext_modules = mypycify([
83
"mypackage/core.py",
84
"mypackage/utils.py",
85
"mypackage/algorithms.py"
86
],
87
opt_level="3",
88
debug_level="1",
89
multi_file=True,
90
strip_asserts=True,
91
verbose=True,
92
target_dir="build",
93
strict_dunder_typing=True
94
)
95
96
# Selective compilation - only compile performance-critical modules
97
ext_modules = mypycify([
98
"mypackage/core.py",
99
"mypackage/utils.py",
100
"mypackage/algorithms.py" # This won't be compiled
101
],
102
only_compile_paths=["mypackage/core.py", "mypackage/utils.py"],
103
opt_level="3"
104
)
105
```
106
107
For complete mypyc usage patterns, see [Command Line Tools](./command-line-tools.md) documentation.