Loading packages

Only the Python standard library and six are available after importing Pyodide. To use other libraries, you’ll need to load their package using either,

  • pyodide.loadPackage for packages built with pyodide.

  • micropip.install for pure Python packages with wheels available on PyPi or on other URLs.

Note

Note that micropip can also be used to load packages built in pyodide (in which case it relies on {ref}pyodide.loadPackage <js_api_pyodide_loadPackage>).

Alternatively you can run Python code without manually pre-loading packages. You can do this with {ref}pyodide.runPythonAsync <api_pyodide_runPythonAsync>) function, which will automatically download all packages that the code snippet imports. It only supports packages included in Pyodide (not on PyPi) at present.

Loading packages with pyodide.loadPackage

Packages can be loaded by name, for those included in the official pyodide repository using,

pyodide.loadPackage('numpy')

It is also possible to load packages from custom URLs,

pyodide.loadPackage('https://foo/bar/numpy.js')

in which case the URL must end with <package-name>.js.

When you request a package from the official repository, all of that package’s dependencies are also loaded. Dependency resolution is not yet implemented when loading packages from custom URLs.

Multiple packages can also be loaded in a single call,

pyodide.loadPackage(['cycler', 'pytz'])

pyodide.loadPackage returns a Promise.

pyodide.loadPackage('matplotlib').then(() => {
  // matplotlib is now available
});

Micropip

Installing packages from PyPI

Pyodide supports installing pure Python wheels from PyPI.

For use in Iodide:

%% py
import micropip
micropip.install('snowballstemmer')

# Iodide implicitly waits for the promise to resolve when the packages have finished
# installing...

%% py
import snowballstemmer
stemmer = snowballstemmer.stemmer('english')
stemmer.stemWords('go goes going gone'.split())

For use outside of Iodide (just Python), you can use the then method on the Promise that micropip.install() returns to do work once the packages have finished loading:

def do_work(*args):
    import snowballstemmer
    stemmer = snowballstemmer.stemmer('english')
    print(stemmer.stemWords('go goes going gone'.split()))

import micropip
micropip.install('snowballstemmer').then(do_work)

Micropip implements file integrity validation by checking the hash of the downloaded wheel against pre-recorded hash digests from the PyPi JSON API.

Installing wheels from arbitrary URLs

Pure python wheels can also be installed from any URL with micropip,

import micropip
micropip.install(
    'https://example.com/files/snowballstemmer-2.0.0-py2.py3-none-any.whl'
)

The wheel name in the URL must follow PEP 427 naming convention, which will be the case if the wheels is made using standard python tools (pip wheel, setup.py bdist_wheel).

All required dependencies need also to be previously installed with micropip or pyodide.loadPackage.

The remote server must set Cross-Origin Resource Sharing (CORS) headers to allow access. Otherwise, you can prepend a CORS proxy to the URL. Note however that using third-party CORS proxies has security implications, particularly since we are not able to check the file integrity, unlike with installs from PyPi.

Example

Adapting the setup from the section on Using Pyodide from Javascript a complete example would be,

<html>
<head>
  <meta charset="utf-8">
</head>
<body>
  <script type="text/javascript">
      // set the pyodide files URL (packages.json, pyodide.asm.data etc)
      window.languagePluginUrl = 'https://cdn.jsdelivr.net/pyodide/v0.16.1/full/';
  </script>
  <script type="text/javascript" src="https://cdn.jsdelivr.net/pyodide/v0.16.1/full/pyodide.js"></script>
  <script type="text/javascript">
    pythonCode = `
      def do_work(*args):
          import snowballstemmer
          stemmer = snowballstemmer.stemmer('english')
          print(stemmer.stemWords('go goes going gone'.split()))

      import micropip
      micropip.install('snowballstemmer').then(do_work)
    `

    languagePluginLoader.then(() => {
      return pyodide.loadPackage(['micropip'])
    }).then(() => {
      pyodide.runPython(pythonCode);
    })
  </script>
</body>
</html>