fermyon / spin-python-sdk

Spin SDK for Python
https://developer.fermyon.com/spin/v2/python-components
Apache License 2.0
31 stars 11 forks source link

spin build not work #87

Closed MominIqbal-1234 closed 7 months ago

MominIqbal-1234 commented 7 months ago

Building component my-first-app with componentize-py -w spin-http componentize app -o app.wasm Traceback (most recent call last): File "/home/myuser/.local/bin/componentize-py", line 8, in sys.exit(script()) AssertionError: failed to read file "wit"

Caused by: No such file or directory (os error 2) Error: Build command for component my-first-app failed with status Exited(1)

karthik2804 commented 7 months ago

Hi @MominIqbal-1234, the error you are seeing is likely due to not having the spin-sdk package installed. You can install the spin-sdk package with the following command:

pip3 install spin-sdk

Let me know if that solves the issue.

MominIqbal-1234 commented 7 months ago

i am follow this doc https://developer.fermyon.com/spin/v2/python-components

but error are same not solved

this is my app code generate default from spin_sdk.http import IncomingHandler, Request, Response

class IncomingHandler(IncomingHandler): def handle_request(self, request: Request) -> Response: return Response( 200, {"content-type": "text/plain"}, bytes("Hello from Python!", "utf-8") )

karthik2804 commented 7 months ago

Could you run the following commands to check where the site-packages on your system is?

python3 -c 'import site; print(site.getsitepackages()[0])'

To check if the spin_sdk is installed could you do the following

ls $(python3 -c 'import site; print(site.getsitepackages()[0])') | grep spin
MominIqbal-1234 commented 7 months ago

thanks so much for your support problem is solved

I run all the commands without a virtual environment but I don't understand why its not work without virtual environment

and can you explain how to deploy .wasm file own server

agardnerIT commented 7 months ago

and can you explain how to deploy .wasm file own server

I'm still learning, so take this with a pinch of salt, but my understanding is that a .wasm file is a compiled bytecode file. Similar to what you'd get in Java if you compiled a .java (source code) file and got a .class file out the other side. .class == .wasm.

So you'll need something run interpret and run that .wasm file. There are many options:

krzysztofwos commented 7 months ago

I am having the same issue. I have spin-sdk and componentize-py installed. I tried invoking the componentize-py manually. It keeps asking for the WIT file.

vscode ➜ /workspaces/exocortex/spin-app/exocortex (main) $ pip list | grep spin
spin-sdk                 3.0.0
vscode ➜ /workspaces/exocortex/spin-app/exocortex (main) $ spin build
Building component http-py-component with `componentize-py -w spin-http componentize app -o app.wasm`
Working directory: "./http-py-component"
Traceback (most recent call last):
  File "/home/vscode/.local/bin/componentize-py", line 8, in <module>
    sys.exit(script())
AssertionError: no WIT files found; please specify the directory or file containing the WIT world you wish to target

Caused by:
    0: failed to read file "wit"
    1: No such file or directory (os error 2)
Error: Build command for component http-py-component failed with status Exited(1)

The examples in the componentize-py repo all point to the wit directory containing appropriate WIT files, e.g., https://github.com/bytecodealliance/componentize-py/blob/main/examples/cli/README.md.

radu-matei commented 7 months ago

@krzysztofwos did you run the commands inside a virtual environment?

$ python3 -m venv venv
$ source venv/bin/activate

$ spin build

@dicej should have more context on why that is required.

krzysztofwos commented 7 months ago

No, I don't use virtual environments. I am developing inside a Dev Container, so I install packages directly. Using a virtual environment makes no difference:

vscode ➜ /workspaces/exocortex/spin-app/exocortex (main) $ python -m venv venv
vscode ➜ /workspaces/exocortex/spin-app/exocortex (main) $ source venv/bin/activate
(venv) vscode ➜ /workspaces/exocortex/spin-app/exocortex (main) $ spin build
Building component http-py-component with `componentize-py -w spin-http componentize app -o app.wasm`
Working directory: "./http-py-component"
Traceback (most recent call last):
  File "/home/vscode/.local/bin/componentize-py", line 8, in <module>
    sys.exit(script())
AssertionError: no WIT files found; please specify the directory or file containing the WIT world you wish to target

Caused by:
    0: failed to read file "wit"
    1: No such file or directory (os error 2)
Error: Build command for component http-py-component failed with status Exited(1)
(venv) vscode ➜ /workspaces/exocortex/spin-app/exocortex (main) $
radu-matei commented 7 months ago

Sorry, I missed the pip3 install -r requirements.txt step in the environment:

$ python3 -m venv venv
$ source venv/bin/activate

$ pip3 install -r requirements.txt
Collecting spin-sdk==3.0.0 (from -r requirements.txt (line 1))
  Obtaining dependency information for spin-sdk==3.0.0 from https://files.pythonhosted.org/packages/7d/1d/455abbc9702557b51c7317d387b158c9b937ef2c1b5b15731bff6bf9e3d6/spin_sdk-3.0.0-py3-none-any.whl.metadata
  Using cached spin_sdk-3.0.0-py3-none-any.whl.metadata (16 kB)
Collecting componentize-py==0.13.1 (from -r requirements.txt (line 2))
  Obtaining dependency information for componentize-py==0.13.1 from https://files.pythonhosted.org/packages/18/9a/a8afa3677a0035da964868a431d3fb6cfcc9e1028f4ac3d462532689a0df/componentize_py-0.13.1-cp37-abi3-manylinux_2_28_aarch64.whl.metadata
  Using cached componentize_py-0.13.1-cp37-abi3-manylinux_2_28_aarch64.whl.metadata (3.4 kB)
Using cached spin_sdk-3.0.0-py3-none-any.whl (94 kB)
Using cached componentize_py-0.13.1-cp37-abi3-manylinux_2_28_aarch64.whl (40.2 MB)
Installing collected packages: spin-sdk, componentize-py
Successfully installed componentize-py-0.13.1 spin-sdk-3.0.0

$ spin build

Let me know if that makes a difference.

(I'm seeing the same behavior reported here on Linux, where I must run the commands inside a virtual environment.)

krzysztofwos commented 7 months ago

It did. spin build works in the virtual environment. That's a bit weird, isn't it?

These are the packages I have in the venv:

(venv) vscode ➜ /workspaces/exocortex/spin-app/exocortex (main) $ pip list
Package         Version
--------------- -------
componentize-py 0.13.1
pip             22.0.2
setuptools      59.6.0
spin-sdk        3.0.0

I have precisely the same versions installed in my user environment.

radu-matei commented 7 months ago

Yeah, that's the same thing I'm seeing as well. Paging @dicej to shed some light on what's happening.

dicej commented 7 months ago

The spin-sdk package contains its own directory of WIT files and a componentize-py.toml file which indicates where that directory is. componentize-py searches for dependencies (including any SDK dependencies with componentize-py.toml and WIT files) by traversing any directories explicitly specified via one or more -p options. In addition, it tries to guess where dependencies are stored using these heuristics: https://github.com/bytecodealliance/componentize-py/blob/35bf29e18b2c1fbaea371903a7d19e4f99b85d2b/src/command.rs#L171-L226

I.e. it first checks if the VIRTUAL_ENV environment variable is set (due to an activated virtual environment). If that's not set, it tries using pipenv --venv. If that doesn't work, it uses python3 -c import site; print(site.getsitepackages()[0]). If none of those work, and no -p options were specified, it uses only the current directory.

So in order to debug why spin-sdk can't be found outside of a virtual environment, the first thing to do is try running python3 -c import site; print(site.getsitepackages()[0]). If that fails or returns an empty string, or if it returns a directory that doesn't contain spin-sdk, then that will explain why it's not working. In that case, hopefully someone with more extensive Python knowledge can tell me the right way to find the directory that has system-wide installed packages like spin-sdk :)

dicej commented 7 months ago

One possible explanation: site.getsitepackages()[0] only gets the first directory, so if there is more than one and spin-sdk is in one of the others, componentize-py won't find it. It should probably print all the directories and add them all to the search path. We can check if that's the problem by running python3 -c import site; print(site.getsitepackages()) and seeing if that prints more than one directory (and if spin-sdk is in one that's not the first).

dicej commented 7 months ago

BTW, happy to debug this myself if someone can provide a way to reproduce it, e.g. via a dev container.

krzysztofwos commented 7 months ago

@dicej Thank you for the explanation.

In the dev container, when I install packages with pip install, they end up in ~/.local/lib/python3.10/site-packages. This explains why Spin acts as if the spin-sdk package was not installed:

vscode ➜ /workspaces/exocortex/spin-app/exocortex (main) $ python3 -c "import site; print(site.getsitepackages()[0])"
/usr/local/lib/python3.10/dist-packages
vscode ➜ /workspaces/exocortex/spin-app/exocortex (main) $ ls /usr/local/lib/python3.10/dist-packages/
vscode ➜ /workspaces/exocortex/spin-app/exocortex (main) $ ls ~/.local/lib/python3.10/site-packages/spin_sdk
componentize-py.toml  http  __init__.py  key_value.py  llm.py  mqtt.py  mysql.py  postgres.py  __pycache__  py.typed  redis.py  sqlite.py  variables.py  wit
vscode ➜ /workspaces/exocortex/spin-app/exocortex (main) $ ls ~/.local/lib/python3.10/site-packages/spin_sdk/wit/
deps  exports  imports  __init__.py  key-value.wit  llm.wit  mqtt.wit  mysql.wit  postgres.wit  __pycache__  rdbms-types.wit  redis.wit  spin.wit  sqlite.wit  types.py  variables.wit

So, to reproduce this, you don't need a dev container. It is sufficient to install spin-sdk without the virtual environment.

An easy fix would be to also add user site-packages to the Python path.

import site
site.getusersitepackages()
'/home/vscode/.local/lib/python3.10/site-packages'
dicej commented 7 months ago

Thanks for digging into this, @krzysztofwos. I think you're right that we should include the user site-packages in the path, and I was able to verify that worked on my Ubuntu 22.04 machine. Interestingly, on MacOS using a Homebrew-installed Python 3.12, it won't let me install packages using pip3 outside of a virtual environment at all:

 $ pip3 install --upgrade componentize-py spin-sdk
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-brew-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip.

    If you wish to install a non-brew packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

Anyway, I'll work on adding the user site-packages to the Python path in componentize-py.

dicej commented 7 months ago

https://github.com/bytecodealliance/componentize-py/pull/81

krzysztofwos commented 7 months ago

Fantastic work, thank you! 🙏

dicej commented 7 months ago

OK, I just released componentize-py 0.13.2 and tested it locally. Please upgrade to that (e.g. pip install --upgrade componentize-py) and let me know how it goes.

krzysztofwos commented 7 months ago

It worked. Thank you for a quick turnaround.

dicej commented 7 months ago

Sounds like it's fixed. Please reopen if this pops up again.