I want to import Keras, but I've tried many ways to do it wrong.

It says at the top you are using ‘Pyolite’. I’m also assuming you are using JupyterLite then? If you go to the JupyterLite page. You’ll see this caution highlighted with high voltage charge symbols to try to indicate its importance to heed:

" Although JupyterLite is currently being developed by core Jupyter developers, the project is still unofficial.
Not all the usual features available in JupyterLab and the Classic Notebook will work with JupyterLite, but many already do!
Don’t hesitate to check out the documentation for more information and project updates."

I’d caution you to not try and use it for what you are trying. (Alternative is offered under ‘Alternative to still not setting up your own machine’ below.) If someone directed you there, then please post the resource here or question them directly with your concerns. My understanding is that keras won’t work in JupyterLite. See here. (More on that below.) Although that linked post is from a year and half ago, I doubt the situation has changed as those libraries are quite complex and can require substantial computational power. On the flip side, things do indeed change and often the information about the limitations isn’t updated until long after. Plus maybe your Pyolite/JupyterLite source is something special. I’m basing much of this on the main JupyterLite resource.

Adressing specifically your ‘tried many ways’:

Also, you should look into how you install things in JupyterLite. The current documentation how to install package is here as part of the documentation. Currently, the command would be recommended as:

%pip install -q keras

And keep in mind that it keeps evolving as the project develops. There seems to be subtleties, too, because if you simply put the magic symbol in the front of your version of the command, your variation actually works at present: try %pip install keras and it shouldn’t give you the same errors. (In fact it seems to install; however doesn’t work. More on that below.) And keep in mind that it keeps evolving as the project develops. The command that it gets translated to and was the only real proper way up to recently is shown further down in the section of the documentation I pointed you at.
(It seems very particular for JupyterLite that right now you explicitly need the symbol. If I run pip install keras, I do indeed see the same error report you post above. Usually, in non-JupyterLite situations, i.e.m with a typical,full Python-based kernel backing, auto-magics is enabled by default and running without a symbol uses the magic variation of the command behind-the-scenes.)

Use of that magic with %pip install is consistent with current best practices in the entire modern Jupyter ecosystem, and so it would be good to adopt to your use going forward when running installs from inside Jupyter cells, see here.

Install works with proper command, but now what…

With the proper command the installation will work in JupyterLite, I believe. It seems it did for me just now in JupyterLite launched from the same site with the warning I pointed you at. However, although it install, it doesn’t seem to have everything it needs to work in this environment. If you now run import keras, you’ll see it needs tensorflow. Here’s a abbreviated version of the traceback you’ll see:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[2], line 1
----> 1 import keras

File /lib/python3.10/site-packages/keras/__init__.py:21
     15 """Implementation of the Keras API, the high-level API of TensorFlow.
     16 
     17 Detailed documentation and user guides are available at
     18 [keras.io](https://keras.io).
     19 """
     20 from keras import distribute
---> 21 from keras import models
     22 from keras.engine.input_layer import Input
     23 from keras.engine.sequential import Sequential

...
File /lib/python3.10/site-packages/keras/engine/functional.py:24
     21 import itertools
     22 import warnings
---> 24 import tensorflow.compat.v2 as tf
     26 from keras import backend
     27 from keras.dtensor import layout_map as layout_map_lib

ModuleNotFoundError: No module named 'tensorflow'

But now you already know the proper command to install a new package so things might seem possible.
Alas, as summarized at the link I pointed you at above, tensorflow doesn’t yet work for tensorflow and at the bottom of what you see if you try to install it with the correct command at this time is:

ValueError: Can't find a pure Python 3 wheel for 'tensorflow'.
See: https://pyodide.org/en/stable/usage/faq.html#micropip-can-t-find-a-pure-python-wheel
You can use `micropip.install(..., keep_going=True)`to get a list of all packages with missing wheels.

At least as recent as August, this was still the same issue and so I think this hurdle still hasn’t been overcome and you cann’t use tensorflow in JupyterLite at this time.

Alternative to still not setting up your own machine to run Keras

You can go the MyBinder Gallery for launches at Gesis here and search ‘keras’ in the search bar on the right side. You’ll see ‘The-Deep-Learning-with-Keras-Workshop’ entry (associated repo: here) listed if you press the launch binder button on the right you’ll get a session where you can import keras in a notebook. (Direct launch link: click here.) Importantly, the session provided has a typical, full Python kernel backing it, along with modern Jupyter, and so a lot of quirks of JupyterLite won’t be present. The sessions are temporary and have limited computational power, and so you’ll need to next determine if it is sufficient for your needs to work through any of what you were trying to work on.


I’d also suggest reading Getting good answers to your questions. A lot of the tips in there are also useful for posting at forums beyond this one. For one thing, screenshots alone generally don’t suffice as a post.



(Aside for the general record: The screenshot in this post made me note that the JupyterLite console still shows Pyolite, although it’s been removed from the notebook kernel indicator now. I haven’t see much about Pyolite in a while. Is the console having that a vestige? Where it used to show ‘Pyolite’ for the kernel indicator when you are viewing a notebook at the time when JupyterLite first came out, it now says ‘Python (Pyodide)’. Showing ‘Python (Pyodide)’ has been the case for several months now. And toggling to choose the kernel in the notebook or when you start the kernel also shows ‘Python (Pyodide)’. Some small inconsistency to fix soon or is Pyolite still the term used behind the scenes for the kernel and should be in the console? Or maybe ‘Python (Pyodide)/Pyolite’ in the console would be clearer and more consistent?)