Create embedded script/entrypoint

27 views
Skip to first unread message

BattleSushi

unread,
Jul 2, 2024, 3:49:12 AMJul 2
to cython-users
I want to use the option to embed Python in my scripts to have (more or less) standalone executable. Consider the following example

```app.py
"""Sample app with complicated dependencies."""
import numpy as np

def main():
    rng = np.random.default_rng()
    print("A random number: ", rng.integers(100))
```

```setup.py
from Cython.Build import BuildExecutable
from Cython.Compiler import Options

# should be after Cython
from setuptools import setup


Options.embed = "main"
BuildExecutable.build("app.py")
setup(
    name="embedded",
    py_modules=[],
    ext_modules=[],
    entry_points={
        "console_scripts": [
            "random = app:main",
        ]
    },
)

```

To run the setup script, I need to have a static library of Python, which I get using `conda install libpython-static`.
But still, `pip install .` yields the following error:
      lto1: fatal error: bytecode stream in file ‘${HOME}/mambaforge/lib/libpython3
.10.a’ generated with LTO version 12.0 instead of the expected 11.3
      compilation terminated.
      lto-wrapper: fatal error: gcc returned 1 exit status
      compilation terminated.
      ${HOME}/mambaforge/compiler_compat/ld: error: lto-wrapper failed
      collect2: error: ld returned 1 exit status


The `--embed` option is sadly not very well document. Has anyone experience, how to correctly do it?

da-woods

unread,
Jul 2, 2024, 1:08:42 PMJul 2
to cython...@googlegroups.com
I think your issue is that the Python static libraries have been build with GCC 12, but you're compiling your own code with GCC 11.3.  It looks like this isn't compatible.

As a word of warning - the embedded more is only really standalone for incredibly simple applications. You won't get numpy bundled into a standalone executable with it.  You'd be better using dedicated tools like pyinstaller which have solved these problems properly.
--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/257436d7-bfc4-4287-b225-e9b6e6ba70d7n%40googlegroups.com.


Reply all
Reply to author
Forward
0 new messages