How do I import modules in a project using pyodide without errors?

4.7k views Asked by At

Whenever I import python modules in a pyodide, it gives this error.

pyodide.js:108 Invalid package name or URI

I am not sure how to properly import modules, I have tried this which was mentioned in the docs.

pyodide.loadPackage('<module address>')    

(this returns a promise on whoes resolution I run this method)

pyodide.runPython('
                   <python code here>
                ')

Upon execution, I get the error mentioned above.

Javascript Code:

<html>
  <head>
      <script type="text/javascript">
          // set the pyodide files URL (packages.json, pyodide.asm.data etc)
          window.languagePluginUrl = 'https://pyodide-cdn2.iodide.io/v0.15.0/full/';
      </script>
      <script src="https://pyodide-cdn2.iodide.io/v0.15.0/full/pyodide.js"></script>
  </head>
  <body>
    Pyodide test page <br>
    Open your browser console to see pyodide output
    <script type="text/javascript">
          languagePluginLoader.then(function () {
            pyodide.loadPackage('<address>').then(() => {
                    console.log(pyodide.runPython('
                    import sys
                    from <my package> import *
                    sys.version
                '));
                console.log(pyodide.runPython('print(1 + 2)'));
            });
            
          });
    </script>
  </body>
</html>

There is a chance that this question might be unclear, but please let me know if you have trouble understanding something.

Also, the string passed in the runPython() method is the python code, just to avoid confusion.

I even tried uploading the module to a server as the docs mentioned a URL using the HTTP protocol, was pretty stupid trying this but I did.

Docs: https://pyodide.readthedocs.io/en/latest/using_pyodide_from_javascript.html#loading-packages

1

There are 1 answers

1
Aray Karjauv On BEST ANSWER

Update: Pyodide v0.21.0

Starting with Pyodide 0.18.0, runPythonAsync does not automatically load packages, so loadPackagesFromImports should be called beforehand.

So, to import a third-party package like numpy we have two options: we can either pre-load required packages manually and then import them in Python

// JS

await pyodide.loadPackage('numpy');
// numpy is now available
pyodide.runPython('import numpy as np')
console.log(pyodide.runPython('np.ones((3, 3)))').toJs())

or we can use the loadPackagesFromImports function that will automatically download all packages that the code snippet imports:

// JS
let python_code = `
import numpy as np
np.ones((3,3))
`
(async () => { // enable await
  await pyodide.loadPackagesFromImports(python_code)
  let result = await pyodide.runPythonAsync(python_code)
  console.log(result.toJs())
})() // call the function immediately

More examples can be found here

(async () => { // enable await
  let python_code = `
import numpy as np
np.ones((3,3))
`
  let pyodide = await loadPyodide();
  await pyodide.loadPackagesFromImports(python_code)
  console.log(pyodide.runPython(python_code).toJs())
})() // call the function immediately
<script src="https://pyodide-cdn2.iodide.io/v0.21.0/full/pyodide.js"></script>

Note also, that starting with version 0.17, Pyodide uses JsProxy for non-JS datatypes. So, before printing the results, it has to be converted using toJs.


Old answer (related to Pyodide v0.15.0)

It is not clear what you are passing as <address> in pyodide.loadPackage('<address>'), but it should just be the package name (e.g. numpy).

Also, note that Pyodide currently supports a limited number of packages. Check out this tutorial on it for more details.

If you want to import a third-party package like numpy there are two options: you can either pre-load required packages manually and then import them in Python using pyodide.loadPackage and pyodide.runPython functions:

pyodide.loadPackage('numpy').then(() => {
  // numpy is now available
  pyodide.runPython('import numpy as np')
  console.log(pyodide.runPython('np.ones((3, 3)))'))
})

Or you can use pyodide.runPythonAsync function that will automatically download all packages that the code snippet imports.

Here is the minimal example for pyodide.runPythonAsync

let python_code = `
import numpy as np
np.ones((3,3))
`
// init environment, then run python code
languagePluginLoader.then(() => {
    pyodide.runPythonAsync(python_code).then(output => alert(output))
})
<script type="text/javascript">
    // Default Pyodide files URL ('packages.json', 'pyodide.asm.data', etc.)
    window.languagePluginUrl = 'https://cdn.jsdelivr.net/pyodide//v0.15.0/full/';
</script>
<script src="https://cdn.jsdelivr.net/pyodide/v0.15.0/full/pyodide.js"></script>