2020-10-31 09:02:23 +00:00
|
|
|
|
(loading_packages)=
|
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-02-06 20:17:57 +00:00
|
|
|
|
- {any}`pyodide.loadPackage` for packages built with pyodide, or
|
2020-10-31 20:00:58 +00:00
|
|
|
|
- `micropip.install` for pure Python packages with wheels available on PyPi or
|
2020-12-31 17:55:27 +00:00
|
|
|
|
from other URLs.
|
2020-10-31 09:02:23 +00:00
|
|
|
|
|
|
|
|
|
```{note}
|
2020-12-31 17:55:27 +00:00
|
|
|
|
`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
|
|
|
|
```
|
|
|
|
|
|
2020-10-31 20:00:58 +00:00
|
|
|
|
Alternatively you can run Python code without manually pre-loading packages.
|
2021-02-06 20:17:57 +00:00
|
|
|
|
You can do this with {any}`pyodide.runPythonAsync`
|
2020-12-31 17:55:27 +00:00
|
|
|
|
which will automatically download all packages that the code snippet imports.
|
|
|
|
|
It only supports packages included in Pyodide (not on PyPi) at present.
|
2020-10-31 09:02:23 +00:00
|
|
|
|
|
|
|
|
|
## Loading packages with pyodide.loadPackage
|
|
|
|
|
|
|
|
|
|
Packages can be loaded by name, for those included in the official pyodide
|
2020-12-31 17:55:27 +00:00
|
|
|
|
repository using e.g.,
|
2020-10-31 09:02:23 +00:00
|
|
|
|
```js
|
|
|
|
|
pyodide.loadPackage('numpy')
|
|
|
|
|
```
|
|
|
|
|
It is also possible to load packages from custom URLs,
|
|
|
|
|
```js
|
|
|
|
|
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.
|
|
|
|
|
|
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
|
|
|
|
|
the dependent.
|
|
|
|
|
|
2020-10-31 09:02:23 +00:00
|
|
|
|
Multiple packages can also be loaded in a single call,
|
|
|
|
|
```js
|
|
|
|
|
pyodide.loadPackage(['cycler', 'pytz'])
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`pyodide.loadPackage` returns a `Promise`.
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
pyodide.loadPackage('matplotlib').then(() => {
|
|
|
|
|
// matplotlib is now available
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
(micropip)=
|
|
|
|
|
## Micropip
|
|
|
|
|
|
|
|
|
|
### Installing packages from PyPI
|
|
|
|
|
|
2020-12-31 12:23:03 +00:00
|
|
|
|
Pyodide supports installing pure Python wheels from PyPI with `micropip`. You
|
|
|
|
|
can use the `then` method on the `Promise` that {func}`micropip.install`
|
|
|
|
|
returns to do work once the packages have finished loading:
|
2019-05-03 17:43:31 +00:00
|
|
|
|
|
2020-05-11 08:06:33 +00:00
|
|
|
|
```py
|
2019-05-03 17:43:31 +00:00
|
|
|
|
def do_work(*args):
|
2020-05-08 21:22:33 +00:00
|
|
|
|
import snowballstemmer
|
|
|
|
|
stemmer = snowballstemmer.stemmer('english')
|
|
|
|
|
print(stemmer.stemWords('go goes going gone'.split()))
|
2019-05-03 17:43:31 +00:00
|
|
|
|
|
|
|
|
|
import micropip
|
|
|
|
|
micropip.install('snowballstemmer').then(do_work)
|
|
|
|
|
```
|
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
|
|
|
|
|
|
|
|
|
Pure python wheels can also be installed from any URL with micropip,
|
|
|
|
|
```py
|
|
|
|
|
import micropip
|
|
|
|
|
micropip.install(
|
|
|
|
|
'https://example.com/files/snowballstemmer-2.0.0-py2.py3-none-any.whl'
|
|
|
|
|
)
|
|
|
|
|
```
|
2020-12-31 17:55:27 +00:00
|
|
|
|
Micropip currently 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
|
2020-05-11 08:34:34 +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`).
|
|
|
|
|
|
2020-05-11 08:38:27 +00:00
|
|
|
|
All required dependencies need also to be previously installed with `micropip`
|
2021-02-06 20:17:57 +00:00
|
|
|
|
or {any}`pyodide.loadPackage`.
|
2020-05-11 08:38:27 +00:00
|
|
|
|
|
2020-12-31 17:55:27 +00:00
|
|
|
|
If the file is on a remote server, it must set Cross-Origin Resource Sharing (CORS) headers to
|
2020-05-11 08:06:33 +00:00
|
|
|
|
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-10-31 09:02:23 +00:00
|
|
|
|
## Example
|
2020-05-08 21:22:33 +00:00
|
|
|
|
|
2020-10-30 20:09:25 +00:00
|
|
|
|
Adapting the setup from the section on {ref}`using_from_javascript`
|
|
|
|
|
a complete example would be,
|
2020-05-08 21:22:33 +00:00
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="utf-8">
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
2020-05-20 16:58:43 +00:00
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
// set the pyodide files URL (packages.json, pyodide.asm.data etc)
|
2020-12-25 21:05:54 +00:00
|
|
|
|
window.languagePluginUrl = 'https://cdn.jsdelivr.net/pyodide/v0.16.1/full/';
|
2020-05-20 16:58:43 +00:00
|
|
|
|
</script>
|
2020-12-25 21:05:54 +00:00
|
|
|
|
<script type="text/javascript" src="https://cdn.jsdelivr.net/pyodide/v0.16.1/full/pyodide.js"></script>
|
2020-05-08 21:22:33 +00:00
|
|
|
|
<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>
|
|
|
|
|
```
|