我有这个代码:
<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/