Table of contents
jupyter notebook ModuleNotFoundError: No module named 'tensorflow'问题
Possibility 1: The tensorflow version does not match the python version
Possibility 2: Jupyter notebook problem (I have this problem)
Possibility 2.1: Install plugin
Run in anoconda3 promt conda terminal:
Possibility 2.2: Install jupyter and python kernel in tensorflow environment (solve problem)
jupyter notebook ModuleNotFoundError: No module named 'tensorflow'问题
Problem importing tensorflow module
Possibility 1: The tensorflow version does not match the python version
- After I activated tensorflow, the python version was 3.9.12, but after entering the python environment directly, the python version was 3.9.7, so I downloaded it again tensorflow2.8.0 of python3.9.7
- But the problem is still not solved. There is no problem with the matching of my tensorflow (2.8.0) and python (3.9.7) versions. Then the problem lies in jupyter.
Possibility 2: Jupyter notebook problem (I have this problem)
Possibility 2.1: Install plugin
-
Run in anoconda3 promt conda terminal:
- Activate tensorflow's virtual environment
activate tensorflow39
- Jupyter Notebook itself uses a Python environment in the root directory of Anaconda by default. If you want to use other virtual environments, you need to use a plug-in, which is the nb_conda plug-in.
conda install nb_conda
- Run jupyter
jupyter notebook
The following problem pops up when running jupyter(If you have not encountered this problem and successfully solved the problem of importing tensorflow in this step, you can skip the following steps) a>
See here to solve this problem
tensorflow still cannot be imported and fails.
Possibility 2.2: Install jupyter and python kernel in tensorflow environment (solve problem)
After installing tensorflow, you must first install the kernel in the tensorflow environment, add the virtual environment to jupyter notebook, and use ipykernel to generate the kernel for the virtual environment.
- Activate environment TensorFlow in conda terminal
activate +你的虚拟环境名字
- Install ipython
conda install ipython
- Install kernel
conda install ipykernel
- Download jupyter in tensorflow environment
conda install jupyter
- Install python kernel for Tensorflow
ipython kernelspec install-self --user
- Add the virtual environment to jupyter notebook and use ipykernel to generate the kernel of the virtual environment
python -m ipykernel install --user --name tensorflow39(与你的tensorflow虚拟环境名称一致)
Reopen jupyter notebook and you will find that you have re-added the newly created tensorflow virtual environment
However, after creating a new tensorflow39 notebook, a new problem came again: it showed that the kernel could not be connected. . . .
Solution: Lower the pyzmq version
pip uninstall pyzmq
pip install pyzmq==19.0.2
Restart jupyternotebook, import tensorflow, and found that it was successful! ! ! !
Reference:
Solve the problem that jupyter notebook cannot connect to the kernel Jupyter notebook code cannot run