Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pettingzoo #15

Open
elliottower opened this issue Mar 1, 2023 · 14 comments
Open

pettingzoo #15

elliottower opened this issue Mar 1, 2023 · 14 comments

Comments

@elliottower
Copy link

elliottower commented Mar 1, 2023

I'm trying to use this package: https://github.com/Farama-Foundation/PettingZoo but I get an error that the package is not found, I think it maybe due to the fact that the module is sometimes capitalized and sometimes lowercase? For example on PyPi it has it listed as pip install PettingZoo (https://pypi.org/project/PettingZoo/) but on the repo itself it's pip install pettingzoo Googling some more though it looks like pip and pypi are case insensitive so this shouldn't matter.

Error on http://localhost:8000/#debug:

1044: requesting module want='pettingzoo' for file=''
1063: repo['-CDN-']='http://localhost:8000/archives/repo/' does not provide want='pettingzoo'

1202: want='pettingzoo' found : False
PKG NOT FOUND : want='pettingzoo', resume=<function at 0x1d3bb68>, ex=ModuleNotFoundError("No module named 'pettingzoo'")

Edit: looking at viblo/pymunk#213 it looks like it might require python 3.11? Some games in pygame rely on pymunk but the one I am trying to is just pettingzoo, pygame, time, and asyncio

Full script I'm trying to run below:

from pettingzoo.classic import connect_four_v3
import time
import pygame
import asyncio

async def main():

    env = connect_four_v3.env(render_mode="human")
    env.reset()

    env.render()  # need to render the environment before pygame can take user input

    for agent in env.agent_iter():
        observation, reward, termination, truncation, info = env.last()
        if termination:
            print(f"Termination ({agent}), Reward: {reward}, info: {info}")
            env.step(None)
        elif truncation:
            print("Truncated")
        else:
            if agent == env.agents[1]:
                # Get user input via the mouse
                while True:
                    event = pygame.event.wait()
                    if event.type == pygame.QUIT:
                        pygame.quit()
                        pygame.display.quit()
                    mousex, mousey = pygame.mouse.get_pos()
                    if mousex < 220:
                        action = 0
                    elif 220 <= mousex < 390:
                        action = 1
                    elif 390 <= mousex < 560:
                        action = 2
                    elif 560 <= mousex < 730:
                        action = 3
                    elif 730 <= mousex < 900:
                        action = 4
                    elif 900 <= mousex < 1070:
                        action = 5
                    else:
                        action = 6
                    env.unwrapped.preview[agent] = action
                    env.render()
                    pygame.display.update()
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        env.unwrapped.preview[agent] = -1
                        break
            else:
                action = env.action_space(agent).sample(mask=observation['action_mask'])
                time.sleep(.25)
            env.step(action)

            await asyncio.sleep(0)  # Very important, and keep it 0

# This is the program entry point:
asyncio.run(main())

# Do not add anything from here
# asyncio.run is non-blocking on pygame-wasm
@pmp-p
Copy link
Member

pmp-p commented Mar 1, 2023

did you try the package yet ?
unzip ( may rename to .zip first ) the https://files.pythonhosted.org/packages/b1/9a/e0884f1a2ec16ea70d420366b37a31a8902ccf5050d2a3e8494611df50cb/PettingZoo-1.22.3-py3-none-any.whl into the game folder and test the module with your game, if success please report for inclusion

@elliottower
Copy link
Author

Thanks, unzipping the files made those errors go away. Is it possible for pygbag to detect modules if they are in a subdirectory? Unzipping all of the required modules into the same folder as main.py makes it pretty messy.

@elliottower
Copy link
Author

elliottower commented Mar 2, 2023

@pmp-p I'm having trouble getting numpy to work, I tried a few different versions but keep getting this error
No module named 'numpy.core._multiarray_umath'

Edit: I was able to get past this error by switching to a venv with python 3.11 and doing pip download numpy==1.23.5 and building from there, but now when executing code in another module (pettingzoo) which depends on numpy I get an error saying No module named 'numpy'. I tested further and if I import numpy in main.py (as the first import) and test np.random.randint(10) it works fine, so it's just a problem with other packages using it.

ys._emscripten_info(emscripten_version=(3, 1, 33), runtime='Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/110.0', pthreads=False, shared_memory=False)
pygame-ce 2.1.4.dev1 (SDL 2.24.2, Python 3.11.2)
284: assets found : 0
341: EventTarget delayed by loader
1020: import scan filename='<stdin>' len(code)=2386 ['numpy', 'pettingzoo']
1044: requesting module want='numpy' for file='<stdin>' 
1055: module want='numpy' requested
1044: requesting module want='pettingzoo' for file='<stdin>' 
1063: repo['-CDN-']='http://localhost:8000/archives/repo/' does not provide want='pettingzoo'
1202: want='numpy' found : True
pkg : http://localhost:8000/archives/repo/pkg/numpy-1.22.4-cp311-cp311-wasm32_mvp_emscripten.whl
 |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| 100.0% 
1291: {req=} in {cls.ignore=} or sys.modules
646: starting shell
996: 72 lines queued for async eval
going interactive
643: TODO detect input/print to select repl debug
>>> /data/data/pygbag/assets/pettingzoo/classic/connect_four/connect_four.py:280: RuntimeWarning: use surfarray: No module named 'numpy'
(ModuleNotFoundError: No module named 'numpy')
  observation = np.array(pygame.surfarray.pixels3d(self.screen))
Task exception was never retrieved
future: <Task finished name='main' coro=<main() done, defined at <console>:18> exception=NotImplementedError("surfarray module not available (ModuleNotFoundError: No module named 'numpy')")>
Traceback (most recent call last):
  File "<console>", line 22, in main
  File "/data/data/pygbag/assets/pettingzoo/utils/wrappers/order_enforcing.py", line 64, in render
    return super().render()
           ^^^^^^^^^^^^^^^^
  File "/data/data/pygbag/assets/pettingzoo/utils/wrappers/base.py", line 88, in render
    return self.env.render()
           ^^^^^^^^^^^^^^^^^
  File "/data/data/pygbag/assets/pettingzoo/utils/wrappers/base.py", line 88, in render
    return self.env.render()
           ^^^^^^^^^^^^^^^^^
  File "/data/data/pygbag/assets/pettingzoo/utils/wrappers/base.py", line 88, in render
    return self.env.render()
           ^^^^^^^^^^^^^^^^^
  File "/data/data/pygbag/assets/pettingzoo/classic/connect_four/connect_four.py", line 280, in render
    observation = np.array(pygame.surfarray.pixels3d(self.screen))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/org.python/assets/site-packages/pygame/__init__.py", line 70, in __getattr__
    raise NotImplementedError(missing_msg)
NotImplementedError: surfarray module not available (ModuleNotFoundError: No module named 'numpy')

@pmp-p
Copy link
Member

pmp-p commented Mar 2, 2023

if modules are in a subfolder "modules" use sys.path.add("modules") before importing them.

Do not forget to put all non stdlib imports at top of main.py and in order

@elliottower
Copy link
Author

Thanks, will try the system path thing tomorrow. I am importing numpy first then the other two in that order, so I’m not sure what the issue would be. I tried changing the imports to have numpy first in one of the pettingzoo files which is giving the module not found error but that didn’t change anything.

Never heard of a single module being imported by one file then somehow not be recognized when used in a different file, but maybe it has to do with asyncio or something.

@pmp-p
Copy link
Member

pmp-p commented Mar 2, 2023

Never heard of a single module being imported by one file then somehow not be recognized when used in a different file, but maybe it has to do with asyncio or something.

The async import of pygbag runtime is a hack, driven by ast module + main.py with no re ordering .

Pypi does not provide a deps table, pyodide's one is borrowed but won't cover new modules manually added.

@elliottower
Copy link
Author

Makes sense, any idea how to solve the issue if the imports are already in order at the top of main?

@pmp-p
Copy link
Member

pmp-p commented Mar 2, 2023

Makes sense, any idea how to solve the issue if the imports are already in order at the top of main?

publish repro case somewhere ( without the build folder ) and link it here so we can look into it.

@elliottower
Copy link
Author

It turned out to be that the underlying env was calling pygame.surfarray but was only importing pygame, and that was leading to issues. When I explicitly imported pygame.surfarray it worked.

@elliottower
Copy link
Author

@pmp-p is it possible to import a local package? I searched through documentation and other issues and didn't find anything. My package is called cathedral-rl and when I run scripts without wasm they work fine, but with wasm it says unknown package cathedral-rl.

1202: want='cathedral_rl' found : False PKG NOT FOUND : want='cathedral_rl', resume=<function <module> at 0x1608c78>, ex=ModuleNotFoundError("No module named 'cathedral_rl'")
from cathedral_rl import cathedral_v0

@pmp-p
Copy link
Member

pmp-p commented Mar 5, 2023

if module is in a subfolder "modules/cathedral_rl" use sys.path.add("modules") before importing. Otherwise just copy the "cathedral_rl" next to your main.py

@elliottower
Copy link
Author

if module is in a subfolder "modules/cathedral_rl" use sys.path.add("modules") before importing. Otherwise just copy the "cathedral_rl" next to your main.py

Is it possible to do relative imports of packages which are up in the directory tree? What I’m importing is the entire repo, as I have the main.py file in a subdirectory examples/pygame-wasm/main.py and the game files are in cathedral_rl/. Maybe if I add an init.py file in the directory of main.py and have it import the specific game files?

I also tried doing pip download gif+[repo] for the entire repository in that modules subfolder, which isn’t ideal but I figured would work, but that also gave the same error about module not found. Sorry for all the questions, very much appreciate your help and effort developing these tools.

@pmp-p
Copy link
Member

pmp-p commented Mar 5, 2023

Is it possible to do relative imports of packages which are up in the directory tree?

No, everything upper level of main.py just does not exists in wasm space which is a virtual filesystem.

if your package to import is out of tree, make it a wheel or a zip that will be mounted at runtime. or put the main.py where it should be ( with a menu to run tests or examples ) eg https://github.com/pmp-p/nurses_2-wasm/tree/pygbag-port

@elliottower
Copy link
Author

Ah I see the problem was that it wasn’t downloading a wheel I think it was just downloading the tree from the repo directly. Will try the wheel approach.

If I have main where it admittedly should be in the root directory, will it be able to do relative imports from other subdirectories? The only reason not to put main in root is that it’s a secondary component of my repo which is mainly the implementation of the game, but not a necessarily a problem to have an extra file in the root directory.

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

No branches or pull requests

2 participants