Skip to content

Conversation

@dicej
Copy link
Collaborator

@dicej dicej commented Feb 3, 2023

spin-python-cli is a stand-alone binary for converting a Python app to a Spin-compatible Wasm module. The intention is that we'll publish this as a Spin plugin, analogous to the JS/TS SDK's js2wasm utility.

The implementation is a bit more complicated than js2wasm because the Python interpreter loads the core library, the app, and its dependencies from disk, so we have to:

  • Embed the core library in the binary and extract it to a temporary directory prior to pre-initializing with Wizer
  • Search for the pipenv site-packages directory and add it to PYTHONPATH
  • Set various environment variables so the Python interpreter knows where to find things (and not buffer stdio)
  • Set another environment variable so the pre-init function knows which module the app lives in

Signed-off-by: Joel Dice joel.dice@fermyon.com

`spin-python-cli` is a stand-alone binary for converting a Python app to a
Spin-compatible Wasm module.  The intention is that we'll publish this as a
`Spin` plugin, analogous to the JS/TS SDK's `js2wasm` utility.

The implementation is a bit more complicated than `js2wasm` because the Python
interpreter loads the core library, the app, and its dependencies from disk, so
we have to:

- Embed the core library in the binary and extract it to a temporary directory prior to pre-initializing with Wizer
- Search for the `pipenv` `site-packages` directory and add it to `PYTHONPATH`
- Set various environment variables so the Python interpreter knows where to find things (and not buffer stdio)
- Set another environment variable so the pre-init function knows which module the app lives in

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
dicej added 3 commits February 3, 2023 15:12
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
Copy link
Member

@radu-matei radu-matei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built from source and tested the sample application included here, everything seems to work great!

One question I have is around type hints, and whether that is something we would like to include.

LGTM!

@dicej
Copy link
Collaborator Author

dicej commented Feb 3, 2023

By "type hints", I assume you mean mypy, right? Or are there other options? I'm pretty new to the world of Python.

Anyway, yes, that would be cool to add.

@lann
Copy link

lann commented Feb 3, 2023

Type hints are baked into the stdlib/language now. See the typing module

Copy link
Contributor

@karthik2804 karthik2804 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great!

@dicej dicej merged commit b14dae3 into spinframework:main Feb 3, 2023
@dicej dicej deleted the py2wasm branch February 3, 2023 23:07
dicej pushed a commit that referenced this pull request Jan 26, 2024
Signed-off-by: karthik2804 <karthik.ganeshram@fermyon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants