|
| 1 | +# Troubleshooting Guide |
| 2 | + |
| 3 | +Common issues and solutions when working with pyz3. |
| 4 | + |
| 5 | +## Build Errors |
| 6 | + |
| 7 | +### Error: unable to load 'pyz3.build.zig': FileNotFound |
| 8 | + |
| 9 | +**Error message:** |
| 10 | +``` |
| 11 | +pyz3.build.zig:1:1: error: unable to load 'pyz3.build.zig': FileNotFound |
| 12 | +pytest.build.zig:2:20: note: file imported here |
| 13 | +const py = @import("./pyz3.build.zig"); |
| 14 | +``` |
| 15 | + |
| 16 | +**Cause:** The `pyz3.build.zig` file is auto-generated and is missing. |
| 17 | + |
| 18 | +**Solution:** |
| 19 | +```bash |
| 20 | +# Run any build command to regenerate it |
| 21 | +poetry install |
| 22 | +# or |
| 23 | +make install |
| 24 | +# or |
| 25 | +poetry run python -m ziglang build install |
| 26 | +``` |
| 27 | + |
| 28 | +The file will be automatically generated from `pyz3/src/pyz3.build.zig`. |
| 29 | + |
| 30 | +**Note:** This file is in `.gitignore` and should not be committed to git. |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +### Error: poetry.lock out of sync |
| 35 | + |
| 36 | +**Error message:** |
| 37 | +``` |
| 38 | +pyproject.toml changed significantly since poetry.lock was last generated. |
| 39 | +Run `poetry lock` to fix the lock file. |
| 40 | +``` |
| 41 | + |
| 42 | +**Cause:** Dependencies in `pyproject.toml` changed but `poetry.lock` wasn't updated. |
| 43 | + |
| 44 | +**Solution:** |
| 45 | +```bash |
| 46 | +make lock |
| 47 | +# or |
| 48 | +poetry lock |
| 49 | + |
| 50 | +# Then commit the updated lock file |
| 51 | +git add poetry.lock |
| 52 | +git commit -m "Update poetry.lock" |
| 53 | +``` |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +### Error: stub files out of date |
| 58 | + |
| 59 | +**Error message:** |
| 60 | +``` |
| 61 | +AssertionError: Contents of example/module.pyi are out of date. |
| 62 | +Please run generate-stubs |
| 63 | +``` |
| 64 | + |
| 65 | +**Cause:** Python stub files (.pyi) are out of sync with Zig module changes. |
| 66 | + |
| 67 | +**Solution:** |
| 68 | +```bash |
| 69 | +make stubs |
| 70 | +# or |
| 71 | +poetry run python -m ziglang build generate-stubs |
| 72 | + |
| 73 | +# Then commit the updated stub files |
| 74 | +git add example/*.pyi |
| 75 | +git commit -m "Update stub files" |
| 76 | +``` |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## Runtime Errors |
| 81 | + |
| 82 | +### ImportError: dynamic module does not define module export function |
| 83 | + |
| 84 | +**Error message:** |
| 85 | +``` |
| 86 | +ImportError: dynamic module does not define module export function (PyInit_modulename) |
| 87 | +``` |
| 88 | + |
| 89 | +**Cause:** The Zig module is missing the `comptime { py.rootmodule(root); }` block. |
| 90 | + |
| 91 | +**Solution:** |
| 92 | +Ensure your Zig module has this at the end: |
| 93 | +```zig |
| 94 | +const py = @import("pyz3"); |
| 95 | +const root = @This(); |
| 96 | +
|
| 97 | +pub fn my_function() !void { |
| 98 | + // Your code |
| 99 | +} |
| 100 | +
|
| 101 | +comptime { |
| 102 | + py.rootmodule(root); |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +**Common mistakes:** |
| 107 | +- Using `pub fn pyz3_module(...)` instead of `py.rootmodule()` (old pattern) |
| 108 | +- Forgetting the `comptime` block entirely |
| 109 | +- Not defining `const root = @This();` |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +## Dependency Issues |
| 114 | + |
| 115 | +### NumPy not found |
| 116 | + |
| 117 | +**Error message:** |
| 118 | +``` |
| 119 | +ModuleNotFoundError: No module named 'numpy' |
| 120 | +``` |
| 121 | + |
| 122 | +**Cause:** NumPy is not installed in the virtual environment. |
| 123 | + |
| 124 | +**Solution:** |
| 125 | +```bash |
| 126 | +# NumPy should be auto-installed with pyz3 |
| 127 | +poetry install |
| 128 | + |
| 129 | +# Or install manually if needed |
| 130 | +poetry add numpy |
| 131 | +make lock |
| 132 | +``` |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +### Zig compiler not found |
| 137 | + |
| 138 | +**Error message:** |
| 139 | +``` |
| 140 | +ModuleNotFoundError: No module named 'ziglang' |
| 141 | +``` |
| 142 | + |
| 143 | +**Cause:** The Zig compiler package is not installed. |
| 144 | + |
| 145 | +**Solution:** |
| 146 | +```bash |
| 147 | +poetry add ziglang |
| 148 | +make lock |
| 149 | +``` |
| 150 | + |
| 151 | +--- |
| 152 | + |
| 153 | +## Test Failures |
| 154 | + |
| 155 | +### All tests fail with "module not found" |
| 156 | + |
| 157 | +**Cause:** Extension modules haven't been built. |
| 158 | + |
| 159 | +**Solution:** |
| 160 | +```bash |
| 161 | +# Build all extension modules |
| 162 | +poetry run python -m ziglang build --build-file ./pytest.build.zig install |
| 163 | + |
| 164 | +# Or use the test script |
| 165 | +./run_all_tests.sh |
| 166 | +``` |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +### Tests pass locally but fail in CI |
| 171 | + |
| 172 | +**Possible causes:** |
| 173 | + |
| 174 | +1. **Lock file out of sync** |
| 175 | + ```bash |
| 176 | + make lock |
| 177 | + git add poetry.lock |
| 178 | + git commit -m "Update poetry.lock" |
| 179 | + ``` |
| 180 | + |
| 181 | +2. **Stub files out of date** |
| 182 | + ```bash |
| 183 | + make stubs |
| 184 | + git add example/*.pyi |
| 185 | + git commit -m "Update stub files" |
| 186 | + ``` |
| 187 | + |
| 188 | +3. **Platform-specific issues** |
| 189 | + - Check if test uses platform-specific code |
| 190 | + - Verify Zig version matches CI |
| 191 | + |
| 192 | +--- |
| 193 | + |
| 194 | +## Memory Issues |
| 195 | + |
| 196 | +### Segmentation fault in tests |
| 197 | + |
| 198 | +**Cause:** Memory corruption or reference counting issue in Zig code. |
| 199 | + |
| 200 | +**Check:** |
| 201 | +1. All `PyObject` references are properly `incref`/`decref` |
| 202 | +2. Using `errdefer` for cleanup in error paths |
| 203 | +3. No use-after-free bugs |
| 204 | +4. Proper alignment in custom allocators |
| 205 | + |
| 206 | +**Debug:** |
| 207 | +```bash |
| 208 | +# Run with AddressSanitizer |
| 209 | +ASAN_OPTIONS=detect_leaks=1 poetry run pytest -v |
| 210 | + |
| 211 | +# Or run Zig tests with safety checks |
| 212 | +zig build test -Doptimize=Debug |
| 213 | +``` |
| 214 | + |
| 215 | +--- |
| 216 | + |
| 217 | +## Getting More Help |
| 218 | + |
| 219 | +If your issue isn't listed here: |
| 220 | + |
| 221 | +1. **Check TODO.md** - Known issues and planned fixes |
| 222 | +2. **Check Issues** - https://github.com/amiyamandal-dev/pyz3/issues |
| 223 | +3. **Open New Issue** - Include: |
| 224 | + - Error message (full output) |
| 225 | + - Python version (`python --version`) |
| 226 | + - Zig version (`zig version` or `poetry run python -m ziglang version`) |
| 227 | + - Operating system |
| 228 | + - Steps to reproduce |
| 229 | + |
| 230 | +--- |
| 231 | + |
| 232 | +## Quick Reference |
| 233 | + |
| 234 | +### Essential Commands |
| 235 | +```bash |
| 236 | +# Installation |
| 237 | +make install # Install dependencies |
| 238 | + |
| 239 | +# Building |
| 240 | +make build # Build package |
| 241 | +make stubs # Generate stub files |
| 242 | +make lock # Update poetry.lock |
| 243 | + |
| 244 | +# Testing |
| 245 | +make test # Run Python tests |
| 246 | +make test-zig # Run Zig tests |
| 247 | +make test-all # Run all tests |
| 248 | + |
| 249 | +# Verification |
| 250 | +make check-stubs # Check stub files |
| 251 | +poetry check --lock # Check poetry.lock |
| 252 | + |
| 253 | +# Cleaning |
| 254 | +make clean # Clean build artifacts |
| 255 | +``` |
| 256 | + |
| 257 | +### Before Committing Checklist |
| 258 | +- [ ] Run `make test-all` - All tests pass |
| 259 | +- [ ] Run `make check-stubs` - Stubs are current |
| 260 | +- [ ] Run `poetry check --lock` - Lock file is synced |
| 261 | +- [ ] Code is formatted (ruff, black) |
| 262 | +- [ ] Commit message follows conventions |
| 263 | + |
| 264 | +--- |
| 265 | + |
| 266 | +*Last updated: 2025-12-22* |
| 267 | +*Version: 0.9.0* |
0 commit comments