Search code examples
pythonbrython

How to import a class using brython?


In recent day I've doing a mini-project (Conway's Game of Life) with python.

I want to do the GUI for the browser, so I decided to use a library called brython, that allow me to use python in the client side (is an interpreter, transforming python code to js code).

The problem I am currently having is that I can't import classes from other file, with the import line.

The structure of folder and files is the next: Game

  • Web
    • Game
      • _ _ init _ _.py
      • Cell.py
      • Game.py
  • index.css
  • index.html
  • index.py

index.py is the file that will be execute when the page is charged. The same has this lines that generate the error (when I commented the error disappear but I can't use what I have to use, lol): import Game.Game as Game

Game.py has a similar line that generate the same error too:

from Game.Cell import Cell

The console in the browser shows the next line: Failed to load resource: the server responded with a status of 404 (File not found)

And this too: Error 404 means that Python module Game was not found at url http://localhost:8000/Game/Web/Game.py

So, the problem is I'm not specifying well the URL, but I don't know how to solve it. Any help?


Solution

  • You should also include the Web forlder to the import. Therefore, your import should be like this: import Web.Game.Game as Game and from Web.Game.Cell import Cell. Also, don't forget to add <script type="text/javascript" src="brython_stdlib.js"></script> in your index.html for these imports to work.

    More in https://brython.info/static_doc/en/import.html.