2020-10-31 09:02:23 +00:00
|
|
|
|
(loading_packages)=
|
2021-07-26 23:00:27 +00:00
|
|
|
|
|
2020-10-31 20:00:58 +00:00
|
|
|
|
# Loading packages
|
2019-05-03 17:43:31 +00:00
|
|
|
|
|
2021-01-03 12:00:31 +00:00
|
|
|
|
Only the Python standard library is available after importing Pyodide.
|
2020-12-31 17:55:27 +00:00
|
|
|
|
To use other packages, you’ll need to load them using either:
|
2021-07-26 23:00:27 +00:00
|
|
|
|
|
2023-02-01 19:00:58 +00:00
|
|
|
|
- {py:func}`micropip.install` (Python) for pure Python packages with wheels as
|
|
|
|
|
well as Pyodide packages (including Emscripten/wasm32 binary wheels). It can
|
|
|
|
|
install packages from PyPI, the JsDelivr CDN or from other URLs.
|
|
|
|
|
- {js:func}`pyodide.loadPackage` (Javascript) for packages built with Pyodide.
|
|
|
|
|
This is a function with less overhead but also more limited functionality.
|
|
|
|
|
micropip uses this function to load Pyodide packages. In most cases you should
|
|
|
|
|
be using micropip.
|
2022-09-07 06:53:55 +00:00
|
|
|
|
|
|
|
|
|
In some cases, and in particular in the REPL, packages are installed implicitly
|
2023-02-01 19:00:58 +00:00
|
|
|
|
from imports. The Pyodide REPL uses {js:func}`pyodide.loadPackagesFromImports`
|
|
|
|
|
to automatically download all packages that the code snippet imports. This is
|
2022-09-07 06:53:55 +00:00
|
|
|
|
useful since users might import unexpected packages in REPL. At present,
|
2023-02-01 19:00:58 +00:00
|
|
|
|
{js:func}`~pyodide.loadPackagesFromImports` will not download packages from
|
|
|
|
|
PyPI, it will only download packages included in the Pyodide distribution. See
|
|
|
|
|
{ref}`packages-in-pyodide` to check the full list of packages included in
|
|
|
|
|
Pyodide.
|
2020-10-31 09:02:23 +00:00
|
|
|
|
|
2022-09-07 06:53:55 +00:00
|
|
|
|
## How to chose between `micropip.install` and `pyodide.loadPackage`?
|
2021-07-26 23:00:27 +00:00
|
|
|
|
|
2023-02-01 19:00:58 +00:00
|
|
|
|
While {py:func}`micropip.install` is written in Python and
|
|
|
|
|
{js:func}`pyodide.loadPackage` in Javascript this has no incidence on when to
|
|
|
|
|
use each of these functions. Indeed, you can easily switch languages using the
|
|
|
|
|
{ref}`type-translations` with,
|
2021-07-26 23:00:27 +00:00
|
|
|
|
|
2022-09-07 06:53:55 +00:00
|
|
|
|
- from Javascript,
|
|
|
|
|
```javascript
|
|
|
|
|
let micropip = pyodide.pyimport(package_name);
|
|
|
|
|
```
|
|
|
|
|
- from Python,
|
|
|
|
|
```
|
|
|
|
|
import pyodide_js
|
|
|
|
|
await pyodide_js.loadPackage('package_name')
|
|
|
|
|
```
|
2020-10-31 09:02:23 +00:00
|
|
|
|
|
2023-02-01 19:00:58 +00:00
|
|
|
|
Instead, the general advice is to use {py:func}`micropip.install` for everything
|
|
|
|
|
except in the following cases where {js:func}`pyodide.loadPackage` might be more
|
2022-09-07 06:53:55 +00:00
|
|
|
|
appropriate,
|
2020-10-31 09:02:23 +00:00
|
|
|
|
|
2022-09-07 06:53:55 +00:00
|
|
|
|
- to load micropip itself,
|
|
|
|
|
- when you are optimizing for size, do not want to install the `micropip`
|
|
|
|
|
package, and do not need to install packages from PyPI with dependency resolution.
|
2021-07-26 23:00:27 +00:00
|
|
|
|
|
2020-10-31 09:02:23 +00:00
|
|
|
|
## Micropip
|
|
|
|
|
|
2022-09-07 06:53:55 +00:00
|
|
|
|
### Installing packages
|
2020-10-31 09:02:23 +00:00
|
|
|
|
|
2022-09-07 06:53:55 +00:00
|
|
|
|
Pyodide supports installing following types of packages with {mod}`micropip`,
|
|
|
|
|
|
|
|
|
|
- pure Python wheels from PyPI with {mod}`micropip`.
|
|
|
|
|
- pure Python and binary wasm32/emscripten wheels (also informally known as
|
|
|
|
|
"Pyodide packages" or "packages built by Pyodide") from the JsDelivr CDN and
|
|
|
|
|
custom URLs.
|
|
|
|
|
{func}`micropip.install` is an async Python function which returns a
|
|
|
|
|
coroutine, so it need to be called with an `await` clause to run.
|
2021-03-31 22:43:46 +00:00
|
|
|
|
|
|
|
|
|
```pyodide
|
2022-06-22 03:15:37 +00:00
|
|
|
|
await pyodide.loadPackage("micropip");
|
|
|
|
|
const micropip = pyodide.pyimport("micropip");
|
|
|
|
|
await micropip.install('snowballstemmer');
|
|
|
|
|
pyodide.runPython(`
|
2021-03-31 22:43:46 +00:00
|
|
|
|
import snowballstemmer
|
|
|
|
|
stemmer = snowballstemmer.stemmer('english')
|
|
|
|
|
print(stemmer.stemWords('go goes going gone'.split()))
|
|
|
|
|
`);
|
2019-05-03 17:43:31 +00:00
|
|
|
|
```
|
2020-05-08 21:22:33 +00:00
|
|
|
|
|
2020-05-11 08:30:25 +00:00
|
|
|
|
Micropip implements file integrity validation by checking the hash of the
|
2021-11-15 09:26:55 +00:00
|
|
|
|
downloaded wheel against pre-recorded hash digests from the PyPI JSON API.
|
2020-05-11 08:06:33 +00:00
|
|
|
|
|
2020-10-30 20:09:25 +00:00
|
|
|
|
(micropip-installing-from-arbitrary-urls)=
|
2020-10-31 09:02:23 +00:00
|
|
|
|
|
|
|
|
|
### Installing wheels from arbitrary URLs
|
2020-05-11 08:06:33 +00:00
|
|
|
|
|
2021-03-20 18:56:10 +00:00
|
|
|
|
Pure Python wheels can also be installed from any URL with {mod}`micropip`,
|
2021-07-26 23:00:27 +00:00
|
|
|
|
|
2020-05-11 08:06:33 +00:00
|
|
|
|
```py
|
|
|
|
|
import micropip
|
|
|
|
|
micropip.install(
|
|
|
|
|
'https://example.com/files/snowballstemmer-2.0.0-py2.py3-none-any.whl'
|
|
|
|
|
)
|
|
|
|
|
```
|
2021-07-26 23:00:27 +00:00
|
|
|
|
|
2022-06-22 03:15:37 +00:00
|
|
|
|
Micropip decides whether a file is a URL based on whether it ends in ".whl" or
|
|
|
|
|
not. The wheel name in the URL must follow [PEP 427 naming
|
2020-05-11 08:06:33 +00:00
|
|
|
|
convention](https://www.python.org/dev/peps/pep-0427/#file-format), which will
|
2021-03-03 08:11:50 +00:00
|
|
|
|
be the case if the wheels is made using standard Python tools (`pip wheel`,
|
2022-06-22 03:15:37 +00:00
|
|
|
|
`setup.py bdist_wheel`). Micropip will also install the dependencies of the
|
|
|
|
|
wheel. If dependency resolution is not desired, you may pass `deps=False`.
|
2020-05-11 08:38:27 +00:00
|
|
|
|
|
2022-02-15 11:20:24 +00:00
|
|
|
|
```{admonition} Cross-Origin Resource Sharing (CORS)
|
|
|
|
|
:class: info
|
|
|
|
|
|
|
|
|
|
If the file is on a remote server, the server must set
|
|
|
|
|
[Cross-Origin Resource Sharing (CORS) headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
|
2022-06-22 03:15:37 +00:00
|
|
|
|
to allow access. If the server doesn't set CORS headers, you can use a CORS proxy.
|
|
|
|
|
Note that using third-party CORS proxies has security implications,
|
2021-03-20 18:56:10 +00:00
|
|
|
|
particularly since we are not able to check the file integrity, unlike with
|
2022-09-07 06:53:55 +00:00
|
|
|
|
installs from PyPI. See [this stack overflow
|
|
|
|
|
answer](https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe/43881141#43881141)
|
|
|
|
|
for more information about CORS.
|
2022-02-15 11:20:24 +00:00
|
|
|
|
```
|
2020-05-11 08:06:33 +00:00
|
|
|
|
|
2020-10-31 09:02:23 +00:00
|
|
|
|
## Example
|
2020-05-08 21:22:33 +00:00
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<html>
|
2021-07-26 23:00:27 +00:00
|
|
|
|
<head>
|
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
2022-08-31 16:43:42 +00:00
|
|
|
|
<script type="text/javascript" src="{{PYODIDE_CDN_URL}}pyodide.js"></script>
|
2021-07-26 23:00:27 +00:00
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
async function main() {
|
2022-03-21 05:44:54 +00:00
|
|
|
|
let pyodide = await loadPyodide();
|
2021-07-26 23:00:27 +00:00
|
|
|
|
await pyodide.loadPackage("micropip");
|
2022-06-22 03:15:37 +00:00
|
|
|
|
const micropip = pyodide.pyimport("micropip");
|
|
|
|
|
await micropip.install("snowballstemmer");
|
|
|
|
|
await pyodide.runPython(`
|
2021-03-31 22:43:46 +00:00
|
|
|
|
import snowballstemmer
|
|
|
|
|
stemmer = snowballstemmer.stemmer('english')
|
|
|
|
|
print(stemmer.stemWords('go goes going gone'.split()))
|
|
|
|
|
`);
|
2021-07-26 23:00:27 +00:00
|
|
|
|
}
|
|
|
|
|
main();
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
2020-05-08 21:22:33 +00:00
|
|
|
|
</html>
|
|
|
|
|
```
|
2021-10-06 07:23:00 +00:00
|
|
|
|
|
2023-02-01 19:00:58 +00:00
|
|
|
|
## Loading packages with {js:func}`pyodide.loadPackage`
|
2022-09-07 06:53:55 +00:00
|
|
|
|
|
|
|
|
|
Packages included in the official Pyodide repository can be loaded using
|
2023-02-01 19:00:58 +00:00
|
|
|
|
{js:func}`~pyodide.loadPackage`:
|
2022-09-07 06:53:55 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
await pyodide.loadPackage("numpy");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
It is also possible to load packages from custom URLs:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
await pyodide.loadPackage(
|
2022-09-13 22:12:40 +00:00
|
|
|
|
"https://foo/bar/numpy-1.22.3-cp310-cp310-emscripten_3_1_13_wasm32.whl",
|
2022-09-07 06:53:55 +00:00
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The file name must be a valid wheel name.
|
|
|
|
|
|
|
|
|
|
When you request a package from the official repository, all of the package's
|
2023-02-01 19:00:58 +00:00
|
|
|
|
dependencies are also loaded. There is no dependency resolution when loading
|
|
|
|
|
packages from custom URLs. If you want dependency resolution for custom URLs,
|
|
|
|
|
use {mod}`micropip`.
|
2022-09-07 06:53:55 +00:00
|
|
|
|
|
|
|
|
|
In general, loading a package twice is not permitted. However, one can override
|
|
|
|
|
a dependency by loading a custom URL with the same package name before loading
|
|
|
|
|
the dependent package.
|
|
|
|
|
|
|
|
|
|
Multiple packages can also be loaded at the same time by passing a list to
|
2023-02-01 19:00:58 +00:00
|
|
|
|
{js:func}`~pyodide.loadPackage`.
|
2022-09-07 06:53:55 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
await pyodide.loadPackage(["cycler", "pytz"]);
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 19:00:58 +00:00
|
|
|
|
{js:func}`~pyodide.loadPackage` returns a {js:class}`Promise` which resolves when all the
|
2022-09-07 06:53:55 +00:00
|
|
|
|
packages are finished loading:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
let pyodide;
|
|
|
|
|
async function main() {
|
|
|
|
|
pyodide = await loadPyodide();
|
|
|
|
|
await pyodide.loadPackage("matplotlib");
|
|
|
|
|
// matplotlib is now available
|
|
|
|
|
}
|
|
|
|
|
main();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
(micropip)=
|
|
|
|
|
|
2021-10-06 07:23:00 +00:00
|
|
|
|
```{eval-rst}
|
|
|
|
|
.. toctree::
|
|
|
|
|
:hidden:
|
|
|
|
|
|
|
|
|
|
packages-in-pyodide.md
|
2023-03-30 08:18:31 +00:00
|
|
|
|
sdl.md
|
2021-11-14 20:47:49 +00:00
|
|
|
|
```
|