我有一个用于 Brython 相关测试的小测试页面,最近在那里添加了 xml.etree.Elementree 模块,但由于某种原因它不起作用。
我有以下代码(实际上还有更多的东西,但我删除了不相关的部分):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="/js/jquery-ui-1.12.1/jquery-ui.min.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/js/jquery-ui-1.12.1/jquery-ui.structure.min.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/layout.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/visual.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/js/jquery-ui-1.12.1/jquery-ui.theme.min.css" media="screen" />
<script rel="script" type="text/javascript" src="/js/jquery-3.1.1.min.js"></script>
<script rel="script" type="text/javascript" src="/js/jquery-ui-1.12.1/jquery-ui.min.js"></script>
<script rel="script" type="text/javascript" src="/js/brython/brython.js"></script>
<script rel="script" type="text/javascript" src="/js/brython/brython_stdlib.js"></script>
</head>
<body onload='brython()'>
<script type="text/python" charset="utf-8">
import sys
from browser import alert, document as docu
from browser import ajax
from xml.etree import ElementTree as ET
def ajaxReceive(req):
alert("Input value: " + docu["numinput"].value)
alert('Ajax response: \n %s' % req.text )
if req.status == 200 or req.status == 0:
d = docu['messagebox']
d.clear()
r = ET.fromstring(req.text)
#n = r.findall('./person/name')
#a = r.findall('./person/age')
#d.text = 'Dude %s is %s old.' % (n,a)
else:
docu['messagebox'] <= 'error: ' + req.text
def ajaxSend():
req = ajax.ajax()
url = '/bryt/'
x = 1
y = 2
z = docu['numinput']
req.open('POST', url, True)
req.bind('complete', ajaxReceive)
req.set_header('content-type', 'application/x-www-form-urlencoded' )
req.send( {
'action': 'calc',
'x': x,
'y': y,
'z': z.value
})
docu['ajaxbutton'].bind('click', ajaxSend )
d = docu['messagebox']
d.clear()
d.text = 'ready'
</script>
<div id='messagebox' style='width: 20%; border: 1px solid gray; padding: 2em;' >
</div>
<br/>
<input id="numinput" / >
<br/>
<br/>
<button id='ajaxbutton' >Ajax run</button>
</body>
</html>
在服务器端,它只将 3 加到给定的数字上。问题是正在接收的 XML 格式的 Ajax 响应。它以清晰的 XML 形式出现,但即使通过调用 .fromstring() 函数来构建 etree 根元素,它的回溯如下:
Error 500 means that Python module pyexpat was not found at url http://example.com/bryt/pyexpat.py
brython.js:7171:1
Error 500 means that Python module pyexpat was not found at url http://example.com/bryt/pyexpat/__init__.py
brython.js:7171:1
Error 500 means that Python module pyexpat was not found at url http://example.com/js/brython/Lib/site-packages/pyexpat.py
brython.js:7171:1
Error 500 means that Python module pyexpat was not found at url http://example.com/js/brython/Lib/site-packages/pyexpat/__init__.py
brython.js:7171:1
Error for module xml.parsers.expat
brython.js:7242:21
Error:
Stack trace:
_b_.ImportError.$factory@http://example.com/js/brython/brython.js line 6466 > eval:49:371
import_hooks@http://example.com/js/brython/brython.js:11605:7
$B.$__import__@http://example.com/js/brython/brython.js:7430:33
$B.$import@http://example.com/js/brython/brython.js:7460:43
$module<@http://example.com/js/brython/brython.js line 7242 > eval:14:9
@http://example.com/js/brython/brython.js line 7242 > eval:1:16
run_py@http://example.com/js/brython/brython.js:7242:1
exec_module@http://example.com/js/brython/brython.js:7276:1
cl_method@http://example.com/js/brython/brython.js:4729:43
import_hooks@http://example.com/js/brython/brython.js:11629:5
$B.$__import__@http://example.com/js/brython/brython.js:7430:33
$B.$import@http://example.com/js/brython/brython.js:7473:5
__init__205@http://example.com/js/brython/brython.js line 7242 > eval:5653:25
type.__call__@http://example.com/js/brython/brython.js:4674:20
factory@http://example.com/js/brython/brython.js:4741:47
XML194@http://example.com/js/brython/brython.js line 7242 > eval:5190:41
ajaxReceive3@http://example.com/js/brython/brython.js line 4294 > eval:176:32
@http://example.com/js/brython/brython.js line 7188 > eval:69:24
ajax.$factory/xmlhttp.onreadystatechange@http://example.com/js/brython/brython.js line 7188 > eval:161:13
brython.js:7243:1
16:21:17.002 args
Array [ "No module named pyexpat" ]
在过去的brython 问题 613中也有类似的情况 ,其中 Pierre 指出没有纯 python pyexpat(2017 年 7 月)之类的东西。但是Brython 标准分发列表 Lib/xml/etree 和 expat.py - 这是否意味着它仍然不可用?
Brython Lib/xml/etreeElementTree.py第 1511 行及以后开始:
class XMLParser:
def __init__(self, html=0, target=None, encoding=None):
try:
from xml.parsers import expat
except ImportError:
try:
import pyexpat as expat
except ImportError:
raise ImportError(
"No module named expat; use SimpleXMLTreeBuilder instead"
)
据我了解,它应该在第一次导入from xml.parsers import expat时成功,但显然它在尝试不存在的 pyexpat 版本时不会成功。
所以,问题是,有没有其他人偶然发现同样的问题和/或有没有人对此有解决方案?
一些额外的(第二天)观察:
克隆和签出一个标签,从 git 存储库构建并没有真正像您期望的那样工作(没有双关语意图)。
% git clone https://github.com/brython-dev/brython.git brython.git
% cd brython.git/scripts
brython.git/scripts% python3 ./make_dist.py
/usr/bin
Traceback (most recent call last):
File "./make_dist.py", line 207, in <module>
run()
File "./make_dist.py", line 88, in run
import make_stdlib_list
File "brython.git/scripts/make_stdlib_list.py", line 53, in <module>
with open(os.path.join(static_doc_folder,lang,'stdlib.html'), 'w', encoding="utf-8") as out:
FileNotFoundError: [Errno 2] No such file or directory: 'brython.git/www/static_doc/en/stdlib.html'
brython.git%
这是由于缺少目录引起的:
brython.git/scripts% mkdir -p ../www/static_doc/{en,es,fr}
brython.git/scripts% python3 ./make_dist.py
最后的构建行是:
adding xml.etree
adding xml.etree.cElementTree
adding xml.parsers
adding xml.parsers.expat
adding xml.sax
所以也许这些都包括在内。
一旦创建了目标,它们(显然,不太确定)就会出现在brython.git/setup/data目录中,为实时网站发布 zip 文件和裸 .js 文件。所以我链接到我的 Apache httpd webroot 中的那个目录。但是那个建筑并没有解决追溯问题。
作为旁注,对于像我这样的老 OpenSource 放屁,我在这个源代码树中感到非常陌生,这个项目是在Mouse camp (Microsoft Windows) 中完成的,甚至我设法找到的一个罕见的 Makefile 不适用于 GNU Make,因为冲突使用空格。更不用说有预期内容的常规安装、自述文件、设置、Makefile 等文件了。我实际上是在阅读资料并猜测这一切应该如何工作。但我想这只能说明 Python 是真正的跨平台语言。
作为一个“开源项目”,它的讨论并不适合所有人,这很有趣: 你加入谷歌群组 brython的申请被拒绝了