python - 如何从 Brython 中的 url 导入 python 库

标签 python brython

我有这个代码:

<script type="text/python" src="https://cdn.jcubic.pl/StringIO.py"></script>
<script type="text/python">
f = StringIO()
</script>

(StringIO 来自 https://svn.python.org/projects/python/trunk/Lib/StringIO.py,删除了 __main__,因为它被执行)

并出现错误:

Traceback (most recent call last):
NameError: name 'StringIO' is not defined

如何从 URL 导入 python 库?有办法吗?

更新发现在Python 3和Brython中StringIO位于io模块中,但我仍然想知道如何从中导入模块如果可能的话请提供网址。

最佳答案

您需要将其放入服务器端 brython/Lib/site-packages 目录中。它自然需要是纯Python。

另请注意,您应该能够使用 pythonpath 设置修改 sys.path 搜索目录:

<body onload="brython(
        debug:1, 
        pythonpath:['http://example.com/brython/Lib/', 'http://e... ]
      )" 
>

因此请注意,这些路径实际上是模块路径的完整 URL。我花了一些时间才把它弄清楚为 documentation (目前为 2018-03-20 brython-3.4.0)没有给出这样的示例。

另请注意,触摸 pythonpath 值会覆盖默认值,您还需要包含它们。据我了解,这些是当前的 Apache PATH_INFO 环境值,即当前加载的路径和 brython 源树 Lib/site-packages。

关于python - 如何从 Brython 中的 url 导入 python 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46789547/

相关文章:

python - 将列表的列表转换为对象

python - 更改 Seaborn 热图颜色条的高度

python - 如果字符串中有空格,则在 telnetlib write 命令中将字符串与文本组合不起作用

javascript - 是否可以在 chrome 中运行 native python 代码?

python - 用于提取 Python 项目/存储库中非内置模块/包列表的工具或脚本?

python - 从具有相同行的向量替换给定索引的numpy数组列中的值

python - Brython 中的 super().__str__() 应该返回不同的东西吗?

ajax - 如何在 Brython 中使用 ajax

javascript - 我可以使用 brython 开发 Chrome 扩展吗?

javascript - 如何将文本输入绑定(bind)到同一 html 页面上的另一个 div