使用 Brython 3.7.5 将 Python 的标准发行版库导入 Angular8 项目

Import a library of Python's standard distribution using Brython 3.7.5 into Angular8 project

我正在用 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 的 stdlib,但 treceback 仍然存在:

ImportError: No module named os

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

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

Q : I see a lot of 404 errors in the browser console when I run Brython scripts, why is that ?

A : this is because of the way Brython implements the "import" mechanism. When a script has to import module X, Brython searches for a file or a package in different directories : the standard library (directory libs for Javascript modules, Lib for Python modules), the directory Lib/site-packages, the directory of current page. For that, Ajax calls are sent to the matching urls; if the file is not found, the 404 error message is written in the browser console, but the error is caught by Brython which goes on searching until it finds the module, or raises ImportError if all paths have been tried with no result

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

有一个页面describes the expected layout;简而言之,这些规则与正常 Python.

的规则非常相似

这是对我有用的解决方案

<!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>