Nuitka / Nuitka

Nuitka is a Python compiler written in Python. It's fully compatible with Python 2.6, 2.7, 3.4-3.12. You feed it your Python app, it does a lot of clever things, and spits out an executable or extension module.
http://nuitka.net
Apache License 2.0
11.33k stars 609 forks source link

--include-distribution-metadata doesn't work with main package: Error, including metadata for distribution without including related package '' #2965

Open JuneStepp opened 1 week ago

JuneStepp commented 1 week ago

I'm trying to access my project's full metadata at runtime.

Running poetry run python -m nuitka example --run (the other options are in the code file) outputs:

Nuitka-Options: Used command line options: --standalone --python-flag=-m --include-distribution-metadata=example example --run
Nuitka: Starting Python compilation with Nuitka '2.3.10' on Python '3.11' commercial grade 'not installed'.
FATAL: Error, including metadata for distribution 'example' without including related package ''.

When not using --include-distribution-metadata, the output is:

Nuitka-Options: Used command line options: --standalone --python-flag=-m example --run
Nuitka: Starting Python compilation with Nuitka '2.3.10' on Python '3.11' commercial grade 'not installed'.
Nuitka: Completed Python level compilation and optimization.                            
Nuitka: Generating source code for C backend compiler.
Nuitka: Running data composer tool for optimal constant value handling.    
Nuitka: Running C compilation via Scons.
Nuitka-Scons: Backend C compiler: gcc (gcc 9).
Nuitka-Scons: Backend linking program with 8 files (no progress information available for this stage).
Nuitka-Scons: Compiled 7 C files using ccache.
Nuitka-Scons: Cached C files (using ccache) with result 'cache hit': 7
Nuitka: Keeping build directory 'example.build'.                               
Nuitka: Successfully created 'example.dist/example.bin'.
Nuitka: Launching 'example.dist/example.bin'.
Traceback (most recent call last):
  File "/media/sf_VM_Stuff/nuitka_test/example.dist/example/__main__.py", line 7, in <module>
RuntimeError: cannot locate package '' associated with metadata

The behavior is the same when not using package mode. I've tired quite a few other variations without any success thus far. Is this how the main package's metadata is supposed to be accessed with Nuitka?

Versions

❯ poetry run python -m nuitka --version
2.3.10
Commercial: None
Python: 3.11.9 (main, Apr  6 2024, 17:59:24) [GCC 9.4.0]
Flavor: Debian Python
Executable: /home/vboxuser/.cache/pypoetry/virtualenvs/example-r5nuuTwf-py3.11/bin/python
OS: Linux
Arch: x86_64
Distribution: Ubuntu (based on Debian) 20.04.6
Version C compiler: /usr/bin/gcc (gcc 9).

Python is installed from this PPA on Ubuntu 20.04. Nuitka is installed with the latest Poetry using their official installer in a virtualenv generated by Poetry. I tried both Nuitka 2.3.10 and develop.

❯ poetry run python -m pip list -v
Package     Version Editable project location      Location                                                                                        Installer
----------- ------- ------------------------------ ----------------------------------------------------------------------------------------------- ------------
example     0.1.0   /media/sf_VM_Stuff/nuitka_test /home/vboxuser/.cache/pypoetry/virtualenvs/example-r5nuuTwf-py3.11/lib/python3.11/site-packages poetry
Nuitka      2.3.10                                 /home/vboxuser/.cache/pypoetry/virtualenvs/example-r5nuuTwf-py3.11/lib/python3.11/site-packages Poetry 1.8.3
ordered-set 4.1.0                                  /home/vboxuser/.cache/pypoetry/virtualenvs/example-r5nuuTwf-py3.11/lib/python3.11/site-packages Poetry 1.8.3
zstandard   0.22.0                                 /home/vboxuser/.cache/pypoetry/virtualenvs/example-r5nuuTwf-py3.11/lib/python3.11/site-packages Poetry 1.8.3
...

Minimal Example

full_example.zip

example/__main__.py:

# The PySide6 plugin covers qt-plugins
# nuitka-project: --standalone
# nuitka-project: --python-flag=-m
# nuitka-project: --include-distribution-metadata=example

import importlib.metadata

print(importlib.metadata.metadata("example"))

pyproject.toml:

[tool.poetry]
name = "example"
version = "0.1.0"
description = "My favorite description"
authors = ["Your Name <you@example.com>"]

[tool.poetry.dependencies]
python = "^3.11"
Nuitka = "^2.3.10"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
kayhayen commented 1 week ago

Compilation vs. source cannot find the metadata as it's only created when you install it. I don't know how poetry works, but I assume you cannot access it either when you run your code via poetry run python -m example either, so how would Nuitka be able to do it.

That said, something about FATAL: Error, including metadata for distribution 'example' without including related package '' seems wrong and you may in fact be able to access it, but "" seems strange for the package name, might be a misdetection of some sorts.

JuneStepp commented 1 week ago

This does work at runtime, because the package is installed in editable mode. This happens automatically with poetry. The pip equivalent is pip install --editable .. See https://setuptools.pypa.io/en/latest/userguide/development_mode.html. The files that end up created within site-packages in this case are example.pth and example-0.1.0.dist-info/.

kayhayen commented 1 week ago

Ok, Nuitka finds them at compile time, the editable ones, but during relaunch the .pth effects that give metadata, that is lost, which is sort of the bug there then. I will look into that, could be easy.