简体   繁体   English

无法在 Pyodide 中导入 python 包

[英]Unable to import a python package in Pyodide

I am trying to import a custom package in Pyodide but it is not working.我正在尝试在Pyodide中导入自定义包,但它不起作用。

The directory structure is目录结构是

index.html
package/
    __init__.py

And my index.html looks like this我的 index.html 看起来像这样

<!DOCTYPE html>
<html>
  <head>
    <script src="https://cdn.jsdelivr.net/pyodide/v0.19.1/full/pyodide.js"></script>
  </head>
  <body>
    Pyodide test page <br />
    Open your browser console to see Pyodide output
    <script type="text/javascript">
      async function main() {
        let pyodide = await loadPyodide({
          indexURL: "https://cdn.jsdelivr.net/pyodide/v0.19.1/full/",
        });

        let mypkg = pyodide.pyimport("package");
        mypkg.main();
      }
      main();
    </script>
  </body>
</html>

Does anyone know why this might be happening?有谁知道为什么会这样?

I am getting the error我收到错误

ModuleNotFoundError: No module named 'package'

In the case of Pyodide, Python runs directly in the browser and as such, it cannot see files in your local file system.对于 Pyodide,Python 直接在浏览器中运行,因此它无法查看本地文件系统中的文件。 It can only import modules in the virtual file system provided by Emscripten .它只能在Emscripten 提供的虚拟文件系统中导入模块。

As described in the Pyodide documentation , you would need to copy your files into the virtual file system for it to work.Pyodide 文档中所述,您需要将文件复制到虚拟文件系统中才能运行。 There are two possibilities,有两种可能,

  • Assuming it's pure Python,假设它是纯 Python,
    1. create a Python wheel wheel out of your package从你的包中创建一个 Python wheel
    2. make it accessible via web server.使其可通过网络服务器访问。 For instance, by placing the wheel in some local folder, and starting the Python web server with python -m http.server in this folder.例如,将轮子放在某个本地文件夹中,然后在该文件夹中使用python -m http.server启动 Python Web 服务器。
    3. Finally, install it with micropip by providing the URL,最后,通过提供 URL 使用 micropip 安装它,
     import micropip await micropip.install('<url-to-the-wheel.whl>') import your_package
  • The other solution is to另一个解决方案是
    1. to create a tar or zip archive with your package,使用您的包创建一个 tar 或 zip 存档,
     zip -r package.zip package/
    1. serve it via some HTTP server (same as above)通过一些 HTTP 服务器提供服务(同上)
    2. Downloading and extracting the archive as described in the documentation ,按照文档中的描述下载并解压缩存档,
       await pyodide.runPythonAsync(` from pyodide.http import pyfetch response = await pyfetch("https://.../your_package.tar.gz") #.zip, .whl, ... await response.unpack_archive() # by default, unpacks to the current dir `) pkg = pyodide.pyimport("your_package"); pkg.do_something();
      If you extract this archive to a different folder than the local folder, you would need to add it to sys.path for Python to find it.如果将此存档解压缩到与本地文件夹不同的文件夹,则需要将其添加到sys.path以便 Python 找到它。

I'm curious about the indexURL argument within the await loadPyodide() statement.我很好奇 await loadPyodide() 语句中的 indexURL 参数。 You have already included the URL within your index.html's head.您已经将 URL 包含在 index.html 的头部。 Is it necessary to include it again within the call to await loadPyodide?是否有必要在等待 loadPyodide 的调用中再次包含它? The documentation shows loadPyodide() being called without any arguments at all, like this:文档显示 loadPyodide() 被调用时根本没有任何参数,如下所示:

let pyodide = await loadPyodide();

https://pyodide.org/en/stable/usage/quickstart.html https://pyodide.org/en/stable/usage/quickstart.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM