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
|
|
|
|
|
|
|
|
|
- {any}`pyodide.loadPackage` for packages built with Pyodide, or
|
|
|
|
|
- {any}`micropip.install` for pure Python packages with wheels available on PyPi or
|
|
|
|
|
from other URLs.
|
2020-10-31 09:02:23 +00:00
|
|
|
|
|
|
|
|
|
```{note}
|
2021-03-20 18:56:10 +00:00
|
|
|
|
{mod}`micropip` can also be used to load packages built in Pyodide (in
|
2021-02-06 20:17:57 +00:00
|
|
|
|
which case it relies on {any}`pyodide.loadPackage`).
|
2020-10-31 09:02:23 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-04-28 06:47:00 +00:00
|
|
|
|
If you use {any}`pyodide.loadPackagesFromImports` Pyodide will automatically
|
|
|
|
|
download all packages that the code snippet imports. This is particularly useful
|
|
|
|
|
for making a repl since users might import unexpected packages. At present,
|
2021-07-27 04:31:59 +00:00
|
|
|
|
{any}`loadPackagesFromImports <pyodide.loadPackagesFromImports>` will not
|
|
|
|
|
download packages from PyPi, it will only download packages included in the
|
|
|
|
|
Pyodide distribution.
|
2020-10-31 09:02:23 +00:00
|
|
|
|
|
2021-03-20 18:56:10 +00:00
|
|
|
|
## Loading packages with {any}`pyodide.loadPackage`
|
2020-10-31 09:02:23 +00:00
|
|
|
|
|
2021-04-06 22:11:00 +00:00
|
|
|
|
Packages included in the official Pyodide repository can be loaded using
|
|
|
|
|
{any}`pyodide.loadPackage`:
|
2021-07-26 23:00:27 +00:00
|
|
|
|
|
2020-10-31 09:02:23 +00:00
|
|
|
|
```js
|
2021-07-26 23:00:27 +00:00
|
|
|
|
pyodide.loadPackage("numpy");
|
2020-10-31 09:02:23 +00:00
|
|
|
|
```
|
2021-07-26 23:00:27 +00:00
|
|
|
|
|
2021-04-06 22:11:00 +00:00
|
|
|
|
It is also possible to load packages from custom URLs:
|
2021-07-26 23:00:27 +00:00
|
|
|
|
|
2020-10-31 09:02:23 +00:00
|
|
|
|
```js
|
2021-07-26 23:00:27 +00:00
|
|
|
|
pyodide.loadPackage("https://foo/bar/numpy.js");
|
2020-10-31 09:02:23 +00:00
|
|
|
|
```
|
2021-07-26 23:00:27 +00:00
|
|
|
|
|
2021-07-27 04:31:59 +00:00
|
|
|
|
The file name in the URL must be `<package-name>.js` and there must be an
|
|
|
|
|
accompanying file called `<package-name>.data` in the same directory.
|
2020-10-31 09:02:23 +00:00
|
|
|
|
|
|
|
|
|
When you request a package from the official repository, all of that package's
|
2021-04-06 22:11:00 +00:00
|
|
|
|
dependencies are also loaded. Dependency resolution is not yet implemented when
|
|
|
|
|
loading packages from custom URLs.
|
2020-10-31 09:02:23 +00:00
|
|
|
|
|
2021-01-12 08:36:25 +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
|
2021-04-06 22:11:00 +00:00
|
|
|
|
the dependent package.
|
2021-01-12 08:36:25 +00:00
|
|
|
|
|
2021-04-06 22:11:00 +00:00
|
|
|
|
Multiple packages can also be loaded at the same time by passing a list to `loadPackage.
|
2021-07-26 23:00:27 +00:00
|
|
|
|
|
2020-10-31 09:02:23 +00:00
|
|
|
|
```js
|
2021-07-26 23:00:27 +00:00
|
|
|
|
pyodide.loadPackage(["cycler", "pytz"]);
|
2020-10-31 09:02:23 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-04-06 22:11:00 +00:00
|
|
|
|
{any}`pyodide.loadPackage` returns a `Promise` which resolves when all of the
|
|
|
|
|
packages are finished loading:
|
2021-07-26 23:00:27 +00:00
|
|
|
|
|
2020-10-31 09:02:23 +00:00
|
|
|
|
```javascript
|
2021-05-23 01:54:27 +00:00
|
|
|
|
let pyodide;
|
2021-07-26 23:00:27 +00:00
|
|
|
|
async function main() {
|
|
|
|
|
pyodide = await loadPyodide({ indexURL: "<some-url>" });
|
|
|
|
|
await pyodide.loadPackage("matplotlib");
|
2020-10-31 09:02:23 +00:00
|
|
|
|
// matplotlib is now available
|
2021-04-06 22:11:00 +00:00
|
|
|
|
}
|
|
|
|
|
main();
|
2020-10-31 09:02:23 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
(micropip)=
|
2021-07-26 23:00:27 +00:00
|
|
|
|
|
2020-10-31 09:02:23 +00:00
|
|
|
|
## Micropip
|
|
|
|
|
|
|
|
|
|
### Installing packages from PyPI
|
|
|
|
|
|
2021-03-31 22:43:46 +00:00
|
|
|
|
Pyodide supports installing pure Python wheels from PyPI with {mod}`micropip`.
|
2021-07-27 04:31:59 +00:00
|
|
|
|
{func}`micropip.install` returns a Python
|
|
|
|
|
[Future](https://docs.python.org/3/library/asyncio-future.html) so you can await
|
|
|
|
|
the future or otherwise use the Python future API to do work once the packages
|
|
|
|
|
have finished loading:
|
2021-03-31 22:43:46 +00:00
|
|
|
|
|
|
|
|
|
```pyodide
|
|
|
|
|
pyodide.runPythonAsync(`
|
|
|
|
|
import micropip
|
|
|
|
|
await micropip.install('snowballstemmer')
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
2021-03-21 16:15:57 +00:00
|
|
|
|
Micropip decides whether a file is a URL based on whether it ends in ".whl" or not.
|
2020-05-11 08:06:33 +00:00
|
|
|
|
The wheel name in the URL must follow [PEP 427 naming
|
|
|
|
|
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`,
|
2020-05-11 08:06:33 +00:00
|
|
|
|
`setup.py bdist_wheel`).
|
|
|
|
|
|
2021-04-06 22:11:00 +00:00
|
|
|
|
All required dependencies must have been previously installed with {mod}`micropip`
|
2021-02-06 20:17:57 +00:00
|
|
|
|
or {any}`pyodide.loadPackage`.
|
2020-05-11 08:38:27 +00:00
|
|
|
|
|
2021-04-06 22:11:00 +00:00
|
|
|
|
If the file is on a remote server, the server must set Cross-Origin Resource Sharing
|
2021-03-20 18:56:10 +00:00
|
|
|
|
(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.
|
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>
|
|
|
|
|
<script
|
|
|
|
|
type="text/javascript"
|
2021-08-03 19:34:14 +00:00
|
|
|
|
src="https://cdn.jsdelivr.net/pyodide/v0.18.0/full/pyodide.js"
|
2021-07-26 23:00:27 +00:00
|
|
|
|
></script>
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
async function main() {
|
|
|
|
|
let pyodide = await loadPyodide({
|
2021-08-03 19:34:14 +00:00
|
|
|
|
indexURL: "https://cdn.jsdelivr.net/pyodide/v0.18.0/full/",
|
2021-07-26 23:00:27 +00:00
|
|
|
|
});
|
|
|
|
|
await pyodide.loadPackage("micropip");
|
|
|
|
|
await pyodide.runPythonAsync(`
|
2021-05-23 01:54:27 +00:00
|
|
|
|
import micropip
|
2021-03-31 22:43:46 +00:00
|
|
|
|
await micropip.install('snowballstemmer')
|
|
|
|
|
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>
|
|
|
|
|
```
|