I have a JupyterLite/pyodide session running a nodebook called "Workshop1.ipynb`. In the same (root) directory as that notebook, I have a file containing useful functions which also does some imports, e.g.
# functions.py
import numpy as np
def a_useful_function(val):
return np.array(val) * 2
I want to call import functions
as the first line of my notebook, to use the functions defined in the notebook, but I then get
ModuleNotFoundError: The module 'numpy' is included in the Pyodide distribution, but it is not installed.
You can install it by calling:
await micropip.install("numpy") in Python, or
await pyodide.loadPackage("numpy") in JavaScript
Is it possible to load modules inside a file which is then imported into the notebook, without calling e.g. import numpy
and/or await micropip.install("numpy")
in the notebook itself?
Is it possible to load modules inside a file which is then imported into the notebook, without calling e.g. import numpy and/or await micropip.install("numpy") in the notebook itself?
The short answer is no. Similar to regular Python you need to indicate which packages should be installed before running your code.
There are now a number of improved ways to do the installations though. micropip.install()
is not needed anymore, and you can use %pip install <package_name>
just like in the rest of the modern Jupyter ecosystem. You can install using %pip install -r requirements.txt
where you have your packages listed in a text file named requirements.txt
with each package on a separate line.
From a technical perspective currently, in Pyodide we can't install a package (which is an async operation ) during the import (which is sync). Unless we recursively parse imports which has a performance overhead.
Beyond that, loading packages that are imported in a pyodide.runPythonAsync
code snippet is a convenient functionality (which JupyterLite is using). However, I don't believe it would be a good idea to extend it to second-level import: i.e. follow your import functions
and see what imports it has. For the same reasons, Python doesn't pip install packages on import in general. Both from the security and reliability perspective it's not great.
In practice, the workaround proposed by @TachyonicBytes if you don't want to specify requirements in a notebook would work.