Pyscript:如何修复“ jsexception(TypeError:未能获取)”尝试从单独文件加载Python脚本时出错?

发布于 2025-01-26 09:08:02 字数 1122 浏览 1 评论 0 原文

尝试使用Pyscript从单独的文件加载Python脚本时,我会收到以下错误:

JsException(TypeError: Failed to fetch)

如何修复此错误并运行Python脚本?

我的源代码由两个文件组成,Hello..html和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!")

在开发人员工具中,控制台显示以下错误:

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.

我正在通过在文件资源管理器中双击它来打开文件。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唱一曲作罢 2025-02-02 09:08:03

发生此错误是因为您尝试使用文件协议方案在本地加载文件。 (换句话说,页面的URL看起来像 file:///path/to/hello.html 。))使用 file> file> file 协议方案,所有主要浏览器禁止在同一文件夹中加载其他文件。相反,您需要

例如,您可以通过在文件夹中运行以下命令在您的文件中设置一个带有Python的测试服务器

python -m http.server

:安装。)

一旦您使用此命令启动本地服务器,您将能够在url “ nofollow noreferrer”> http:// localhost:8000/hello.html 。当您以这种方式访问​​它时,您的浏览器会看到hello.html和hello.py。都来自相同 origin ,因此它可以使您从hello.html加载hello.py。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文