Fix #2304 - Make pyimport work as expected (#2311)

* Fix #2304 - Make pyimport work as expected

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Andrea Giammarchi
2025-03-10 16:28:42 +01:00
committed by GitHub
parent f2bbc6ed5f
commit caeab77a8e
5 changed files with 50 additions and 1 deletions

View File

@@ -33,6 +33,7 @@ const hooks = {
config = parse(text);
}
if (config.packages) {
await wrap.interpreter.loadPackage("micropip");
const micropip = wrap.interpreter.pyimport("micropip");
await micropip.install(config.packages, {
keep_going: true,

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="../../../dist/core.css">
<script type="module" src="../../../dist/core.js"></script>
</head>
<body>
<div id="status">Status:</div>
<canvas id="canvas" width="200" height="200"></canvas>
<script type="py-game" src="./main.py" config="./pyscript.toml"></script>
</body>
</html>

View File

@@ -0,0 +1,34 @@
import sys
print("Starting test...")
# Try NumPy
try:
import numpy as np
arr = np.array([1, 2, 3])
print(f"NumPy works: {arr.mean()}")
except Exception as e:
print(f"NumPy error: {e}")
# Try PyGame without NumPy first
try:
print("Testing PyGame...")
import pygame
screen = pygame.display.set_mode((200, 200))
screen.fill((255, 0, 0)) # Fill with red
pygame.display.flip()
print("PyGame works!")
except Exception as e:
print(f"PyGame error: {e}")
# Now try PyGame with NumPy
try:
print("Testing PyGame+NumPy...")
color_array = np.random.randint(0, 255, size=(50, 50, 3), dtype=np.uint8)
surface = pygame.surfarray.make_surface(color_array)
screen.blit(surface, (75, 75))
pygame.display.flip()
print("PyGame+NumPy integration works!")
except Exception as e:
print(f"PyGame+NumPy integration error: {e}")
print("Test completed")

View File

@@ -0,0 +1,2 @@
name = "PyGame Numpy Minimal Example Copy"
packages = [ "numpy", ]