Debian Codegraph @

docs/how-to/troubleshooting.rst

raw
1 #################
2 Troubleshooting
3 #################
4
5 This guide helps you resolve common issues when using build.
6
7 ***************************************
8 Build fails with missing dependencies
9 ***************************************
10
11 **Symptom**: Build fails with errors like "ModuleNotFoundError" or "No module named 'X'".
12
13 **Cause**: Your build backend or its dependencies aren't installed in the isolated environment.
14
15 **Solution 1**: Let build handle it automatically (default):
16
17 .. code-block:: console
18
19 $ python -m build
20
21 Build will read your ``pyproject.toml`` and install all required dependencies.
22
23 **Solution 2**: If using ``--no-isolation``, manually install dependencies:
24
25 .. code-block:: console
26
27 $ pip install setuptools your-build-backend
28 $ python -m build --no-isolation
29
30 **********************************************
31 "Unmet dependencies" with ``--no-isolation``
32 **********************************************
33
34 **Symptom**: A ``--no-isolation`` build stops with:
35
36 ::
37
38 ERROR Unmet dependencies (checked against /usr/local/bin/python3.9):
39 anndata>=0.7.4
40 wanted: >=0.7.4
41 found: not installed
42
43 **Cause**: build checks the declared requirements against the interpreter named in the header (the one running build),
44 not against a virtual environment or your system package manager. Read each entry as:
45
46 - ``found: not installed`` - the package is invisible to *that* interpreter's metadata. If you believe it is installed
47 (for example via a Linux distribution or FreeBSD port), it was installed for a different Python, so build cannot see
48 it. Install it for the interpreter in the header, or run that interpreter's ``python -m build``.
49 - ``found: <version>`` - the package is installed but its version does not satisfy ``wanted``. Upgrade or downgrade it
50 to match.
51
52 **Solution 1**: Install or fix the offending dependency for the interpreter shown in the header.
53
54 **Solution 2**: If you manage build dependencies externally (distribution packaging), skip the check entirely:
55
56 .. code-block:: console
57
58 $ python -m build --no-isolation --skip-dependency-check
59
60 See :doc:`basic-usage` for more on ``--skip-dependency-check``.
61
62 *******************************************
63 Find the backend version for a bug report
64 *******************************************
65
66 **Symptom**: A backend's issue tracker asks for the exact version of the backend you used. ``pyproject.toml`` lists only
67 the requirement specifiers (``setuptools >= 40.8.0``), and on an isolated build the resolved version lives in the
68 temporary environment that build deletes afterwards.
69
70 **Solution**: Read it from build's output. After installing the build dependencies, build prints the resolved version of
71 each one:
72
73 .. code-block:: console
74
75 $ python -m build --wheel
76 ...
77 * Getting build dependencies for wheel...
78 * Installed build dependency versions:
79 - setuptools==82.0.1
80 * Building wheel...
81
82 Copy the ``name==version`` line for the backend into your report. With ``--no-isolation`` build installs nothing, so
83 read the version from the active interpreter instead (for example ``pip show setuptools``).
84
85 *******************************
86 Build hangs or appears frozen
87 *******************************
88
89 **Symptom**: Build command runs but produces no output for a long time.
90
91 **Possible causes**:
92
93 1. **Waiting for authentication**: If you're using a private package index, build may be waiting for credentials.
94
95 See :doc:`corporate-environments` for authentication setup.
96
97 2. **Large download**: Build backend dependencies are being downloaded.
98
99 Use ``-v`` or ``-vv`` for verbose output:
100
101 .. code-block:: console
102
103 $ python -m build -vv
104
105 3. **Build backend is actually running**: Some backends (especially for C extensions) can take time.
106
107 Check system monitor for CPU/disk activity to confirm work is happening.
108
109 *************************************
110 SSL certificate verification failed
111 *************************************
112
113 **Symptom**: Errors like "SSL: CERTIFICATE_VERIFY_FAILED" or "certificate verify failed".
114
115 **Cause**: Your system doesn't trust the SSL certificate of the package index.
116
117 **Solution 1**: Provide the CA certificate (recommended):
118
119 .. code-block:: console
120
121 $ export PIP_CERT=/path/to/company-ca-bundle.crt
122 $ python -m build
123
124 **Solution 2**: Use build with virtualenv for better SSL support:
125
126 .. code-block:: console
127
128 $ pip install build[virtualenv]
129 $ python -m build
130
131 **Solution 3**: For development only, disable verification (not recommended):
132
133 .. code-block:: console
134
135 $ export PIP_TRUSTED_HOST=pypi.company.com
136 $ python -m build
137
138 See :doc:`corporate-environments` for more details.
139
140 **************************
141 Permission denied errors
142 **************************
143
144 **Symptom**: "PermissionError" or "Access is denied" when creating directories or files.
145
146 **Common causes**:
147
148 1. **Output directory is protected**: The ``dist/`` directory is owned by another user or process.
149
150 Solution: Remove the directory first or use a different output location:
151
152 .. code-block:: console
153
154 $ rm -rf dist/
155 $ python -m build
156
157 Or:
158
159 .. code-block:: console
160
161 $ python -m build --outdir /tmp/my-build
162
163 2. **Running inside a read-only directory**: The source directory is on a read-only filesystem.
164
165 Solution: Copy the source to a writable location first.
166
167 3. **Antivirus software**: Some antivirus tools block Python from creating virtual environments.
168
169 Solution: Add Python and your project directory to antivirus exclusions.
170
171 ********************************************
172 Wheel is not compatible with this platform
173 ********************************************
174
175 **Symptom**: "is not a supported wheel on this platform" when trying to install the built wheel.
176
177 **Cause**: The wheel was built for a different Python version or platform.
178
179 **Solution**: Build on the same platform and Python version where you'll install, or use cibuildwheel for multi-platform
180 builds.
181
182 See :doc:`choosing-tools` for when to use cibuildwheel vs build.
183
184 *********************************************
185 Build succeeds but package is missing files
186 *********************************************
187
188 **Symptom**: After building, the package installs but is missing source files, data files, or modules.
189
190 **Cause**: Files aren't included in the source distribution manifest.
191
192 **Solution**: The fix depends on your build backend:
193
194 **For setuptools** (`setuptools documentation <https://setuptools.pypa.io/>`_):
195
196 Add a ``MANIFEST.in`` file or use ``package_data`` in ``pyproject.toml``:
197
198 .. code-block:: toml
199
200 [tool.setuptools.package-data]
201 mypackage = ["data/*.json", "templates/*.html"]
202
203 **For hatchling** (`hatchling documentation <https://hatch.pypa.io/latest/config/build/>`_):
204
205 Use the ``include`` and ``exclude`` options:
206
207 .. code-block:: toml
208
209 [tool.hatch.build.targets.wheel]
210 include = [
211 "src/**/*.py",
212 "src/**/*.json",
213 ]
214
215 **For flit** (`flit documentation <https://flit.pypa.io/>`_):
216
217 Flit includes all files tracked by `git <https://git-scm.com/>`_ by default. Ensure files are committed.
218
219 **Test your sdist**: Always verify the sdist contains all necessary files:
220
221 .. code-block:: console
222
223 $ python -m build --sdist
224 $ tar -tzf dist/mypackage-1.0.0.tar.gz
225
226 ****************************************
227 ModuleNotFoundError in the built wheel
228 ****************************************
229
230 **Symptom**: The wheel installs successfully, but importing the package fails with "ModuleNotFoundError".
231
232 **Cause**: The package structure in your source doesn't match what the build backend expects.
233
234 **Common issues**:
235
236 1. **Missing ``__init__.py``**: Ensure every package directory has an ``__init__.py`` file (not needed for namespace
237 packages).
238 2. **Wrong directory structure**: For src-layout, ensure you have ``src/packagename/`` not just ``packagename/``.
239 3. **Not configuring package discovery**: Tell your build backend where to find packages.
240
241 For setuptools with src-layout:
242
243 .. code-block:: toml
244
245 [tool.setuptools.packages.find]
246 where = ["src"]
247
248 *********************************************
249 Version conflict between build dependencies
250 *********************************************
251
252 **Symptom**: Build fails with errors about conflicting package versions.
253
254 **Cause**: Build dependencies have incompatible version requirements.
255
256 **Solution 1**: Update your build backend and its dependencies:
257
258 .. code-block:: console
259
260 $ pip install --upgrade pip setuptools
261
262 Then try building again:
263
264 .. code-block:: console
265
266 $ python -m build
267
268 **Solution 2**: If using ``--no-isolation``, create a fresh virtual environment:
269
270 .. code-block:: console
271
272 $ python -m venv fresh-env
273 $ source fresh-env/bin/activate # On Windows: fresh-env\\Scripts\\activate
274 $ pip install build
275 $ python -m build
276
277 The isolated build (without ``--no-isolation``) avoids this issue by creating a clean environment each time.
278
279 *************************************
280 Build works locally but fails in CI
281 *************************************
282
283 **Common causes**:
284
285 1. **Different Python versions**: CI may use a different Python version than your local environment.
286
287 Solution: Specify Python version in CI config, test locally with that version.
288
289 2. **Missing system dependencies**: C extensions may need system libraries.
290
291 Solution: Install required libraries in CI before building:
292
293 .. code-block:: yaml
294
295 # GitHub Actions example
296 - name: Install system dependencies
297 run: |
298 sudo apt-get update
299 sudo apt-get install -y libffi-dev
300
301 3. **File path issues**: Windows vs Unix path separators.
302
303 Solution: Use ``pathlib.Path`` in ``pyproject.toml`` configurations and build scripts.
304
305 4. **Git not available**: Some backends use git for version detection.
306
307 Solution: Ensure git is installed in CI, or use explicit versioning in ``pyproject.toml``.
308
309 See :doc:`ci-cd` for CI-specific guidance.
310
311 *******************************
312 Catching deprecation warnings
313 *******************************
314
315 To catch warnings from your build backend in CI:
316
317 .. code-block:: console
318
319 $ PYTHONWARNINGS=error::DeprecationWarning python -m build
320
321 Or for setuptools specifically:
322
323 .. code-block:: console
324
325 $ PYTHONWARNINGS=error:::setuptools.config.setupcfg python -m build
326
327 This fails the build if deprecated features are used, helping you catch issues early.
328
329 *************************
330 Build backend not found
331 *************************
332
333 **Symptom**: "Backend 'X' is not available" or "No module named 'X'".
334
335 **Cause**: The build backend specified in ``pyproject.toml`` isn't installed.
336
337 **Solution**: This shouldn't happen with isolated builds (the default). If you see this:
338
339 1. Check your ``pyproject.toml`` ``[build-system]`` section is correct:
340
341 .. code-block:: toml
342
343 [build-system]
344 requires = ["hatchling"]
345 build-backend = "hatchling.build"
346
347 2. If using ``--no-isolation``, install the backend:
348
349 .. code-block:: console
350
351 $ pip install hatchling
352 $ python -m build --no-isolation
353
354 *******************************************
355 No build-system section in pyproject.toml
356 *******************************************
357
358 **Symptom**: Build succeeds but you're not sure which backend is being used.
359
360 **Cause**: No ``[build-system]`` section is specified in ``pyproject.toml``.
361
362 **Solution**: As recommended in :PEP:`517`, if no backend is specified, build will fallback to
363 ``setuptools.build_meta:__legacy__``. This is for backward compatibility with older projects. For new projects,
364 explicitly specify your build backend:
365
366 .. code-block:: toml
367
368 [build-system]
369 requires = ["hatchling"]
370 build-backend = "hatchling.build"
371
372 This makes your build configuration explicit and avoids relying on the fallback behavior.
373
374 ***************************************
375 Build installed in user site-packages
376 ***************************************
377
378 **Symptom**: Virtual environment creation fails with errors like "executable /usr/local/bin/python missing".
379
380 **Cause**: When build is installed in the user site-packages (``pip install --user build``), it can interfere with
381 isolated environment creation.
382
383 **Solution**: Install build in a virtual environment instead:
384
385 .. code-block:: console
386
387 $ python -m venv build-env
388 $ source build-env/bin/activate # On Windows: build-env\\Scripts\\activate
389 $ pip install build
390 $ python -m build
391
392 Or use ``pipx`` to install build in isolation:
393
394 .. code-block:: console
395
396 $ pipx install build
397 $ pipx run build
398
399 ***************************************************
400 Package name has dashes but wheel has underscores
401 ***************************************************
402
403 **Symptom**: Your package is named ``my-package`` but the wheel is ``my_package-1.0.0-py3-none-any.whl``.
404
405 **This is expected behavior**: According to `PEP 427 <https://peps.python.org/pep-0427/>`_, wheel filenames normalize
406 package names by replacing dashes with underscores. This is not a bug.
407
408 Your users can still install with either name:
409
410 .. code-block:: console
411
412 $ pip install my-package # Works
413 $ pip install my_package # Also works
414
415 The package metadata preserves your original name, only the filename is normalized.
416
417 **************************
418 Getting more information
419 **************************
420
421 Verbose output
422 ==============
423
424 Use ``-v`` or ``-vv`` for detailed output:
425
426 .. code-block:: console
427
428 $ python -m build -v # Verbose
429 $ python -m build -vv # Very verbose
430
431 This shows what build is doing and can help identify where failures occur.
432
433 Check your configuration
434 ========================
435
436 Verify your ``pyproject.toml`` is valid:
437
438 .. code-block:: console
439
440 $ python -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb')))"
441
442 Test the build backend directly
443 ===============================
444
445 You can test if your build backend works independently:
446
447 .. code-block:: console
448
449 $ pip install your-build-backend
450 $ python -m your_build_backend
451
452 Inspect the build environment
453 =============================
454
455 build removes its isolated environment when the build ends. To keep it for inspection, point ``--env-dir`` at an empty
456 location:
457
458 .. code-block:: console
459
460 $ python -m build --env-dir .build-env
461
462 build removes that location after a successful build and keeps it after a failure, so a failed build leaves the
463 environment in ``.build-env`` for you to examine. Setting ``TMPDIR`` (or ``TEMP`` on Windows) only moves the temporary
464 directory; it does not keep the environment around.
465
466 build forwards the backend's output to your terminal as it happens, so compilation logs and error messages are already
467 visible. Raise the verbosity to add build's own diagnostics and stream the environment setup live:
468
469 .. code-block:: console
470
471 $ python -m build -vv
472
473 .. _debug-a-failed-build:
474
475 **********************
476 Debug a failed build
477 **********************
478
479 When a backend fails, build prints a ``TIP`` line that links back here. Work through these steps to gather what you
480 need.
481
482 Stream the backend output
483 =========================
484
485 build forwards everything the backend writes to standard output and error straight to your terminal, so compiler
486 invocations and backend tracebacks already appear without any extra flag. Raise the verbosity to add build's own
487 diagnostics and to stream the environment setup (installing the backend and its dependencies) live instead of only on
488 failure:
489
490 .. code-block:: console
491
492 $ python -m build -vv
493
494 Keep the build environment and sources
495 ======================================
496
497 A failed build deletes its temporary working directories before you can look at them. Pin both so they survive the run:
498
499 .. code-block:: console
500
501 $ python -m build --env-dir .build-env --sdist-extract-dir .build-src
502
503 ``--env-dir`` keeps the isolated environment (the installed backend and its dependencies) on failure; see `Inspect the
504 build environment`_. ``--sdist-extract-dir`` keeps the extracted sdist, which is the directory the backend actually runs
505 in, so any files the backend leaves next to your sources stay put.
506
507 Find the backend's own logs
508 ===========================
509
510 build cannot capture log *files* the backend writes inside its own working directory, because PEP 517 gives the frontend
511 no way to discover them. Each backend exposes its own setting for keeping them. For ``meson-python`` and
512 ``scikit-build``, point its build directory at a path you control:
513
514 .. code-block:: console
515
516 $ python -m build -C build-dir=_build
517
518 The backend then usually writes its build tree and logs under ``_build``. Check your backend's documentation for the
519 equivalent setting.
520
521 **********************
522 Still having issues?
523 **********************
524
525 If your issue isn't covered here:
526
527 1. Check the `issue tracker <https://github.com/pypa/build/issues>`_ for similar problems
528 2. Enable verbose output (``-vv``) and include the full output when reporting issues
529 3. Include your ``pyproject.toml`` and Python version when asking for help
530
531 **********
532 See also
533 **********
534
535 - :doc:`corporate-environments` for proxy, SSL, and authentication issues
536 - :doc:`ci-cd` for CI/CD-specific problems
537 - :doc:`../reference/environment-variables` for environment variables that affect build