pyxel icon indicating copy to clipboard operation
pyxel copied to clipboard

Unable to import classes from different files using https://kitao.github.io/pyxel/wasm/launcher/?run=

Open dComposer opened this issue 1 year ago • 7 comments

Hi -- I'm trying to use the "https://kitao.github.io/pyxel/wasm/launcher/?run=" command to run a game I just built that's stored in my GitHub repository but it's failing at importing some classes that are stored in different python files. Does the main.py file have to be one giant file for this to work or do I have to explicitly write somewhere that it should look for these import files?

The game I'm trying to run is located here: https://github.com/dComposer/pyxel_breakout This is the command I'm trying to run: https://kitao.github.io/pyxel/wasm/launcher/?run=dComposer.pyxel_breakout.main

This is the error I'm getting in my web browser: Traceback (most recent call last): File "/lib/python311.zip/_pyodide/_base.py", line 499, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 340, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 3, in File "/lib/python3.11/site-packages/pyxel/cli.py", line 167, in run_python_script runpy.run_path(python_script_file, run_name="main") File "", line 291, in run_path File "", line 98, in _run_module_code File "", line 88, in _run_code File "main.py", line 3, in from ball import Ball ModuleNotFoundError: No module named 'ball'

Thanks so much!

dComposer avatar Dec 23 '23 16:12 dComposer

A potential workaround.

# Force download of relative modules from jsdelivr before importing.
for module_path in ("ball.py", "paddle.py"):
    open(module_path).close()

import ball
import paddle

Pyxel tries to download from jsdelivr whenever a missing file in the Pyxel working directory is accessed. This can be triggered by opening the module as a file, so that they are available for import. Not the neatest way to go, but might work. For deeper hierachies using packages, it might be necessary to invalidate the cache first: https://pyodide.org/en/stable/usage/faq.html#why-can-t-i-import-a-file-i-just-wrote-to-the-file-system.

BoniLindsley avatar Dec 26 '23 16:12 BoniLindsley

Does that work or is it just an idea? Kitao uploads files to jsdeliver, but people making games are not adding their module to the same location.

merwok avatar Dec 26 '23 18:12 merwok

Just an idea. Haven't tested it fully.

Another idea can be to download source tarball and extract it. Not something jsdelivr provides, from what I can tell.

BoniLindsley avatar Dec 26 '23 19:12 BoniLindsley

Thanks for the suggestions, but I'm still getting this ModuleNotFound error:

  File "/lib/python311.zip/_pyodide/_base.py", line 499, in eval_code
    .run(globals, locals)
     ^^^^^^^^^^^^^^^^^^^^
  File "/lib/python311.zip/_pyodide/_base.py", line 340, in run
    coroutine = eval(self.code, globals, locals)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "", line 3, in 
  File "/lib/python3.11/site-packages/pyxel/cli.py", line 167, in run_python_script
    runpy.run_path(python_script_file, run_name="__main__")
  File "", line 291, in run_path
  File "", line 98, in _run_module_code
  File "", line 88, in _run_code
  File "main.py", line 3, in 
    from ball import Ball
ModuleNotFoundError: No module named 'ball'

dComposer avatar Dec 29 '23 03:12 dComposer

It seems to be fine by the time I have tried iit. If it happens again, you might try purging the jsDelivr CDN cache. https://www.jsdelivr.com/tools/purge. To find the URLs to purge, before clicking to start in the launcher, open up the browser console (usually ctrl+shift+I on Windows or Linux)

Copied 'https://cdn.jsdelivr.net/gh/dComposer/pyxel_breakout/main.py' to '/pyxel_working_directory/main.py' [pyxel.js:209:13]

As for debugging whether this is the issue, the error says that it is trying to import on line 3. But you can see it is not on line 3 anymore https://github.com/dComposer/pyxel_breakout/blob/cbd0b91f5ce619daf54646e31b1de6c41403eb34/main.py

  File "main.py", line 3, in 
    from ball import Ball

So this suggests the old main.py script was still being used.

BoniLindsley avatar Dec 29 '23 11:12 BoniLindsley