Reputation: 2079
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?
Upvotes: 1
Views: 483
Reputation: 11201
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.
Upvotes: 1
Reputation: 1143
I reproduced your problem on the current live jupyterlite. I would honestly be surprised if this is not actually a bug in jupyterlite, that will be resolved in future iterations.
The workaround I found is to do some of the problematic imports in the notebook itself, if that is a possibility. This is the cleanest solution.
There is another solution, taken even by the numpy.org website itself, which is to use the ?code=CODE
URL paramter. You can see some examples here. If you enter the browser console, you can see that this is the URL the numpy website uses:
https://jupyterlite.github.io/demo/repl/?toolbar=1&kernel=python&code=import%20numpy%20as%20np
Of course, you can write spaces in the URL itself, so that this becomes:
https://jupyterlite.github.io/demo/repl/?toolbar=1&kernel=python&code=import numpy as np
This does not put you right into your notebook, but it provides a way to run code before-hand, maybe you can use it to solve your problem.
Upvotes: 3