0

我正在使用 Angular 和 Brython 进行第一次实验。一切都开始工作了,然后奇怪的是 Python 的标准库没有被识别。我很想知道为什么。

这是 html 部分(angular8 中的index.html ):

<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>HelloWorld</title>
        <base href="/">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="icon" type="image/x-icon" href="favicon.ico">
    </head>
    <body onload = "brython()">
        <app-root></app-root>

        <script type="text/python">
            import os
        </script>
    </body>
</html>

这些是angular.json文件中链接的脚本

"scripts": [
    "src/assets/js/script.js",
    "src/assets/js/brython.js",
    "src/assets/js/brython_stdlib.js"
]

为什么,尽管链接(大概)Python's stdlib,回溯仍然存在:

ImportError: No module named os

是我忘记放别的东西了,还是这个问题无法解决?从某种意义上说,您不能在 angular8 中使用 Brython?

4

2 回答 2

0

我怀疑https://brython.info/static_doc/en/faq.html中描述的搜索机制失败了:

问:当我运行 Brython 脚本时,我在浏览器控制台中看到很多 404 错误,这是为什么?

答:这是因为 Brython 实现“导入”机制的方式。当脚本必须导入模块 X 时,Brython 会在不同目录中搜索文件或包:标准库(Javascript 模块的目录 libs,Python 模块的 Lib)、目录 Lib/site-packages、当前页面目录. 为此,Ajax 调用被发送到匹配的 url;如果找不到该文件,则在浏览器控制台中写入 404 错误消息,但该错误被 Brython 捕获,它会继续搜索直到找到该模块,或者如果所有路径都已尝试但没有结果,则会引发 ImportError

这意味着安装不完全正确。你检查控制台是否有错误?

有一个页面描述了预期的布局;简而言之,这些规则与普通 Python 的规则非常相似。

于 2019-10-10T10:42:55.290 回答
0

这是对我有用的解决方案

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>BrythonAngular</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!--  Brython imports-->
  <script type="text/javascript"
    src="https://cdn.jsdelivr.net/npm/brython@3.9.3/brython.min.js">
</script>
<script type="text/javascript"
    src="https://cdn.jsdelivr.net/npm/brython@3.9.3/brython_stdlib.js">
</script>
</head>
<body class="mat-typography" onload = "brython()">
  <app-root></app-root>
  <script type="text/python">
  import os
  print(dir(os))
  </script>
</body>
</html>
于 2021-05-26T12:10:36.257 回答