'JsException(TypeError: Failed to fetch)

When I try to import a pyscript source code to my HTML it shows a "JsException(TypeError: Failed to fetch)" error.

helloworld.py

print("Hello World")

testPyscript.html

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
    <title></title>
</head>
<body>
    <py-script src="helloworld.py">
        ("Another Text Test")
    </py-script>
</body>
</html>


Solution 1:[1]

I was having the same problem and found the answer here: PyScript: Loading Python Code in the Browser

The problem is <py-script src="helloworld.py"> do not support loading local files, you need a server for the browser to load it...

Go into the folder you keep the files and run python -m http.server 80 and then, on the browser, go to localhost/testPyscript.html

Hope it helps

Solution 2:[2]

For some reason your directory which contains helloworld.py and testPyscript.html need to run in localhost, open your folder in vsCode and install live server from extensions then in your right bottom corner press on Go Live. you will be directed to the default browser with the expected output from helloworld.py

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vinicius Franco
Solution 2