Skip to content

Installing Python Packages

In addition to most of the Standard Library, many other Python pacakges can loaded and used in the Browser. There are three types of Packages that can be loaded:

  • Packages that have been pre-built by the Pyodide team, with their C/Rust/Fortran extensions pre-compiled to work in Web Assembly.
  • Pure Python packages that are available on PyPI; that is, packages which publish a wheel ending in -none-any.whl. (Some packages written only in Python do not publish Pure Python wheels; check the 'Download Files' section on PyPI to confirm.)
  • Pure Python wheels that you build yourself.

To install a new Python package in PyScript, include the name of the package or the URL of the self-built wheel in the packages list in code <py-config>

<py-config>
    packages = ['astropy', 'pyyaml', 'https://example.com/files/someWheel-py3-none-any.whl']
    # packages can be from PyPI, Pyodide Packages, or self-built wheels
</py-config>

To install a new Micropython package in PyScript, include the name of the package in the packages list in code <mpy-config>.

<mpy-config>
    packages = ['curses.ascii']
</mpy-config>

To list of strings in packages are passed invidiually to the Micropython's mip tool. This means that by default packages are looked up in the micropthon-lib repository, but you can also install directly from a URL, or, using the shorthand demo'd below, from a GitHub repo:

<mpy-config>
    packages = [
        'http://example.com/x/y/foo.mpy', # Download from URL
        'github:org/user/path/package.json' # Download from GH Repo
        ]
</mpy-config>

To install a new Python package from PyPI in Pyodide, first install the micropip python package, then use micropip.install(package_name). For example, to load the Pure Python package pyyaml, and use it in some Python code:

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

    await pyodide.loadPackage("micropip");
    const micropip = pyodide.pyimport("micropip");
    await micropip.install('pyyaml');

    // Once installed, packages can be imported and used
    pyodide.runPython(`
        import yaml
        document = """
        a: 1
        b:
            c: 3
            d: 4
        """
        y = yaml.safe_load(document)
        print(yaml.dump(y))
    `)
    };
main();

To load a package built by the Pyodide project, use pyodide.loadPackage:

    //...
    await pyodide.loadPackage("astropy");
    //...