Jupyter can't import tensorflow

I recently fixed my Jupyter not working by installing the lower version of traitlets 5.9.0 and now my Jupyter cannot import tensorflow. I get this error when I run the import command:

ModuleNotFoundError Traceback (most recent call last)
/var/folders/lt/6t5nlthj5rd0v2vrh58xknqm0000gn/T/ipykernel_66673/3459245449.py in
7 from PIL import Image
8 import numpy as np
----> 9 import tensorflow as tf
10 import pprint
11 from public_tests import *

ModuleNotFoundError: No module named ‘tensorflow’

Your fix of the other issue has unintended consequences. Be careful. It looks where you are working isn’t where tensorflow is installed on your machine. It may be already on your machine since you act like that import worked previously.

An option if you don’t want to jump through the hurdles of connecting your current Jupyter kernel to where tensorlow is/was:

Run the following if you use Anaconda/conda in a new cell in the notebook where you want to run that import:

%conda install conda-forge::tensorflow

After that run successfully, restart the kernel and try the import statement.
If you don’t use Anconda/conda try as the install command to run in the notebook:

%pip install tensorflow

Then restart the kernel after it completes successfully and try and the import statement.

Correct me if I am wrong, but is

a temporary fix? If so, how do I make it so that any and all Jupyter environments do not have this issue again

It should be permanent for the environment you run it in. Assuming you are using a local installation and that will affect it.

got it!
Currently running that line of code in a cell. It’s taking a while; is that normal?
Well to be specific, I started running the cell when you suggested it

Conda is slow. A lot of people prefer mamba or pixi. You could have a really complex environment that you set up?

1 Like

Update:
After restarting the kernel (it took around 1 day to complete)the same problem persists. Any other fixes?

Did you try with %pip install? It is not recommended because conda should be your primary package manager, but it seems you caused issues when you lowered the traitlets version, and so it may be worth trying at this point.

Tried that, installed successfully (not without uninstalling an existing version of numpy:
Attempting uninstall: numpy
Found existing installation: numpy 1.21.5
Uninstalling numpy-1.21.5:
Successfully uninstalled numpy-1.21.5
ERROR: pip’s dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
daal4py 2021.6.0 requires daal==2021.4.0, which is not installed.
numba 0.55.1 requires numpy<1.22,>=1.18, but you have numpy 1.26.4 which is incompatible.
Successfully installed astunparse-1.6.3 flatbuffers-24.3.7 gast-0.5.4 google-auth-oauthlib-1.2.0 google-pasta-0.2.0 keras-2.15.0 libclang-16.0.6 ml-dtypes-0.2.0 numpy-1.26.4 opt-einsum-3.3.0 tensorboard-2.15.2 tensorflow-2.15.0 tensorflow-estimator-2.15.0 tensorflow-io-gcs-filesystem-0.36.0 termcolor-2.4.0
Note: you may need to restart the kernel to use updated packages.)
But running the cell takes way longer than it should. Till now it has not finished running

Did you restart everything since trying pip and try again?

And your installed version of numpy was quite old, it isfrom 2021. Do you have your based Conda environment already documented and your conda environments? If so, it seems like it may be time to clean out your old Anaconda installation and reinstall the more current Anaconda Distirbution. Then make a new environment in & install tensorflow in it using conda.

I restarted the kernell, yes. When you

what do you mean by this and how do I go about finding out?

Would you mind walking me through this? I hope I am not asking too much. I’m basically a newbie to using python packages

If you just were getting started with Python then you probably don’t have too much invested in your Conda environments. Then just read on the Anaconda Distribution page about uninstalling and cleaning. Then read about cleaning out old pip installs and then install the Anaconda Distribution from scratch.

Try Jupyter Notebook and JupyterLab there to see Jupyter is working. (Remember this all went bad when you tried fixing Jupyter not working messing with traitlets.) Hopefully Jupyter works for both Jupyter Notebook and JupyterLab. Good place to be.

Now you need tensorflow.
Read about making environments in general here and here to try and understand what is going on.
Then read How to Install Tensorflow with Anaconda on Windows but adapt it some. Specifically: I wouldn’t suggest installing a new version of Python in the environment unless it doesn’t work. You just installed new Anaconda Distribution and so don’t update pip as it should be what Anaconda wants.

So now you should have an environment with tensorflow installed.
Next you want to be able to use Jupyter witih that environment. Activate the environment and follow here. (You may want to read the answer here, too, for more understanding and guidance.) Now you should be able to start Jupyter up (whatever interface your prefer) and choose the kernel associated with that environment where you installed tensorflow.
Now you should be able to import tensorflow in Jupyter.