简体   繁体   English

“未找到导入 tensorflow 模块”仅在 jupyter 笔记本上,但不在 jupyter 实验室或终端上

[英]"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.我启动 powershell anaconda 提示并激活新项目的环境。 Then I install tensorflow using the command provided by the tensorflow website pip install tensorflow .然后我使用 tensorflow 网站pip install tensorflow

To validate that the installation was successful, I open python from within the terminal and import tensorflow as such:为了验证安装是否成功,我从终端中打开 python 并导入 tensorflow :

> 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.一切似乎都在工作,我继续启动一个 jupyter notebook 并在一个新的单元格中执行导入。 When I run the cell import tensorflow , however, I receive the following error:但是,当我运行 cell import tensorflow时,我收到以下错误:

---------------------------------------------------------------------------
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. 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.当我在 jupyterlab 下运行 import 语句时,一切正常。

What is going on?到底是怎么回事?

  1. Why does the import statement work in the terminal and under jupyter lab为什么import语句在终端和jupyter实验室下有效
  2. Why doesn't it work in jupyter notebook?为什么它在 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.在 jupyter 实验室和笔记本电脑上,我运行了命令!conda info并且可以确认安装 tensorflow 的环境确实处于活动状态。

Follow these steps install Tenosrflow on Virtual environment with PIP按照以下步骤使用 PIP 在虚拟环境中安装 Tenosrflow

#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)以下是我在机器上解决问题的根本原因和步骤(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. Python 3.8.x 作为命令行开发工具的一部分安装在我的机器上。 It is because Python was installed when I tried to install xcode-select by running the command code-select --install .这是因为当我尝试通过运行命令code-select --install安装 xcode-select 时安装了 Python 。 That means the Python's main installation is at /Library/Developer/CommandLineTools/Frameworks/Python.framework/Versions (see the docs ).这意味着 Python 的主要安装位于/Library/Developer/CommandLineTools/Frameworks/Python.framework/Versions (请参阅文档)。 Therefore, when creating a virtual environment, Python paths will be loaded from the location mentioned above.因此,在创建虚拟环境时,将从上述位置加载 Python 路径。 In the meantime, I installed Jupyter notebook via the brew package manager.同时,我通过 brew package manager 安装了 Jupyter notebook。 Consequently, they (Python and Jupyter) cannot talk to each other.因此,它们(Python 和 Jupyter)无法相互通信。

How to fix the issue如何解决问题

Removed the installation of Jupyter notebook which has been installed via brew.删除了已通过 brew 安装的 Jupyter notebook 的安装。 Then installed it inside the specifically virtual environment where my project is located.然后将其安装在我的项目所在的特定虚拟环境中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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