在 Pyodide 的类中找不到导入的模块

问题描述 投票:0回答:1

我正在尝试将 pyodide 与 lxml 和 urllib3 一起使用,由于某些我不明白的原因,当我尝试在一个应该是 lxml etree 解析器的类中使用

urllib3
时,我收到错误
NameError: name 'urllib3' is not defined

示例代码位于 https://martin-honnen.github.io/pyodide-tests/simple-requests-test-case3.html(请注意,所有输出都会发送到浏览器控制台,因此请使用 F12 查看输出).

代码正在执行

<script src="https://cdn.jsdelivr.net/pyodide/v0.26.0/full/pyodide.js"></script>
然后


    const python = `
import js

import urllib3

import lxml

from lxml import etree as ET

url = base_url + 'foo-transform-module.xsl'

js.console.log(urllib3.request('GET', url).status)

class TestResolver(ET.Resolver):
    def resolve(self, url, id, context):
        print("Resolving URL '%s'" % url)
        if url.startswith('http'):
            return self.resolve_file(urllib3.request('GET', url), context)
        else:
            return False


parser = ET.XMLParser(no_network=False)
parser.resolvers.add(TestResolver())

tree = ET.parse(url, parser)

tree.getroot().tag
  `;

    async function main() {
        let pyodide = await loadPyodide();

        await pyodide.loadPackagesFromImports(python);

        const locals = new Map();
        locals.set('base_url', window.location.href.replace(/[^/]+?$/, ''));

        console.log(await pyodide.runPythonAsync(python, { locals : locals }));
    };

    main();

控制台中的完整错误是

Uncaught (in promise) PythonError: Traceback (most recent call last):
  File "/lib/python312.zip/_pyodide/_base.py", line 574, in eval_code_async
    await CodeRunner(
  File "/lib/python312.zip/_pyodide/_base.py", line 394, in run_async
    coroutine = eval(self.code, globals, locals)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<exec>", line 28, in <module>
  File "src/lxml/etree.pyx", line 3570, in lxml.etree.parse
  File "src/lxml/parser.pxi", line 1952, in lxml.etree._parseDocument
  File "src/lxml/parser.pxi", line 1978, in lxml.etree._parseDocumentFromURL
  File "src/lxml/parser.pxi", line 1881, in lxml.etree._parseDocFromFile
  File "src/lxml/parser.pxi", line 1200, in lxml.etree._BaseParser._parseDocFromFile
  File "src/lxml/parser.pxi", line 633, in lxml.etree._ParserContext._handleParseResultDoc
  File "src/lxml/parser.pxi", line 739, in lxml.etree._handleParseResult
  File "src/lxml/etree.pyx", line 329, in lxml.etree._ExceptionContext._raise_if_stored
  File "src/lxml/parser.pxi", line 462, in lxml.etree._local_resolver
  File "src/lxml/docloader.pxi", line 150, in lxml.etree._ResolverRegistry.resolve
  File "<exec>", line 20, in resolve
NameError: name 'urllib3' is not defined

    at new_error (pyodide.asm.js:10:9965)
    at pyodide.asm.wasm:0x16dbeb
    at pyodide.asm.wasm:0x177339
    at _PyEM_TrampolineCall_JS (pyodide.asm.js:10:125866)
    at pyodide.asm.wasm:0x1c2db7
    at pyodide.asm.wasm:0x2c7b17
    at pyodide.asm.wasm:0x20a78c
    at pyodide.asm.wasm:0x1c34a4
    at pyodide.asm.wasm:0x1c37b3
    at pyodide.asm.wasm:0x1c3831
    at pyodide.asm.wasm:0x29e865
    at pyodide.asm.wasm:0x2a4e5c
    at pyodide.asm.wasm:0x1c3971
    at pyodide.asm.wasm:0x1c35da
    at pyodide.asm.wasm:0x17699d
    at callPyObjectKwargs (pyodide.asm.js:10:64068)
    at Module.callPyObjectMaybePromising (pyodide.asm.js:10:65316)
    at wrapper (pyodide.asm.js:10:27006)
    at onGlobalMessage (pyodide.asm.js:10:101760)

我的代码有什么缺陷吗?如何让导入的模块在类的代码中被识别?

python lxml urllib3 pyodide
1个回答
0
投票

我不确定,但看起来它可以运行

lxml
并且需要使用
urllib3

控制台还显示

loading lxml, urllib3
,这可能表明它首先加载
lxml
,然后加载
urllib3

在选项卡中

Network
我还看到它首先加载
lxml-5.2.1-cp312-cp312-pyodide_2024_0_wasm32.whl
然后加载
urllib3-2.2.1-py3-none-any.whl
- 也许这就是问题所在。

当我将它作为两个字符串运行时

const python = `
import js
import urllib3
`

await pyodide.loadPackagesFromImports(python);

const second = `
import lxml
// ... other code ...
`

await pyodide.loadPackagesFromImports(second);

然后看起来它加载正确。

© www.soinside.com 2019 - 2024. All rights reserved.