1

When trying to load a Python script from a separate file using PyScript, I get the following error:

JsException(TypeError: Failed to fetch)

How can I fix this error and run the Python script?

My source code consists of two files, hello.html and hello.py.

hello.html:

<html>
  <head>
    <title>Hello PyScript</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
  </head>
  <py-env>
- paths:
  - /hello.py
  </py-env>
  <body>
    <py-script src="./hello.py"></py-script>
  </body>
</html>

hello.py:

print("Hello, PyScript!")

In the developer tools, the console displays the following error:

Access to fetch at 'file:///hello.py' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

I am opening the file by double-clicking on it in file explorer.

6
  • It is not a Pyscript issue. It is a because a are running the file directly. You need a webserver like nginx, httpd or any othe for development stage Commented May 7, 2022 at 11:40
  • 1
    No, it's not a PyScript issue, at least not directly. But it does look like a PyScript issue to beginners, so I wrote this question and answer so that people would find this page when searching about their problem. Commented May 9, 2022 at 4:44
  • 1
    Indeed, it may be the first error that a Pyscript beginner may face ! Commented May 9, 2022 at 12:54
  • 1
    @RichVel: Did you also have a CORS Issue? I got this introduced since today, already made an issue on Github. http.server did not solve it for me Commented May 13, 2022 at 17:43
  • 1
    The code above does need a local HTTP server for both the HTML file and hello.py to fix this CORS issue. The similar looking issue that @DataMastery and I had with download of pyscript.js was actually unrelated to this - it was fixed by the PyScript project on 13th May, the same day it was reported. Commented May 14, 2022 at 18:06

1 Answer 1

2

This error occurs because you are trying to load the file locally, using the file protocol scheme. (In other words, the URL of the page looks something like file:///path/to/hello.html.) When using the file protocol scheme, all major browsers disallow loading other files in the same folder for security reasons. Instead, you need to set up a local server to host your files.

For example, you can set up a test server with Python by running the following command in the folder your files are in:

python -m http.server

(This assumes you have Python 3 installed, and the python command points to your Python 3 installation.)

Once you have started your local server with this command, you will be able to access hello.html at the URL http://localhost:8000/hello.html. When you access it this way, your browser will see that hello.html and hello.py both come from the same origin, so it will allow you to load hello.py from hello.html.

Sign up to request clarification or add additional context in comments.

2 Comments

but also i get the same error when i use:- <py-env> - math </py-env>
@aymhenry The math module is in the standard library, so you should be able to use import math in a py-script tag without using a py-env tag.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.