简体   繁体   中英

"Importing tensorflow module not found" Only on jupyter notebook but not jupyter lab or terminal

I launch the powershell anaconda prompt and activate an environment for a new project. Then I install tensorflow using the command provided by the tensorflow website pip install tensorflow .

To validate that the installation was successful, I open python from within the terminal and import tensorflow as such:

> python
Python 3.7.10 (default, Feb 26 2021, 13:06:18) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf
<module 'tensorflow' from 'C:\\Users\\myname\\Anaconda3\\envs\\test_env\\lib\\site-packages\\tensorflow\\__init__.py'>

Everything seems to be working, I proceed to launch a jupyter notebook and perform the import in a new cell. When I run the cell import tensorflow , however, I receive the following error:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-3-d6579f534729> in <module>
----> 1 import tensorflow

ModuleNotFoundError: No module named 'tensorflow'

To double check, I close the jupyter notebook session, and launch a jupyter lab session instead - all from the same Anaconda powershell terminal with the correct environment activated. Everything works correctly when I run the import statement under jupyterlab.

What is going on?

  1. Why does the import statement work in the terminal and under jupyter lab
  2. Why doesn't it work in jupyter notebook?
  3. How do I fix it?

[Edit:]

On both the jupyter lab and the notebook I ran the command !conda info and can confirm that the environment in which tensorflow was installed was indeed active.

Follow these steps install Tenosrflow on Virtual environment with PIP

#Install virtualenv
sudo pip3 install virtualenv
#Create virtual environment name: venv
virtualenv venv
#Activate venv
source venv/bin/activate
#Install tensorflow
venv$ pip3 install tensorflow
#Install Jupyter notebook
venv$ pip3 install notebook
# Launch jupyter notebook
venv$ jupyter notebook

Below are the root cause and steps I have resolved the issue on my machine (macOS Monterey 12.5.1)

The root cause on my case

Python 3.8.x was installed on my machine as a part of the Command Line Developer Tools. It is because Python was installed when I tried to install xcode-select by running the command code-select --install . That means the Python's main installation is at /Library/Developer/CommandLineTools/Frameworks/Python.framework/Versions (see the docs ). Therefore, when creating a virtual environment, Python paths will be loaded from the location mentioned above. In the meantime, I installed Jupyter notebook via the brew package manager. Consequently, they (Python and Jupyter) cannot talk to each other.

How to fix the issue

Removed the installation of Jupyter notebook which has been installed via brew. Then installed it inside the specifically virtual environment where my project is located.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM