简体   繁体   English

从终端运行脚本时无法导入 tensorflow,即使 tensorflow 在 jupyter 笔记本和终端中工作

[英]Can't import tensorflow when run script from terminal, even though tensorflow works in jupyter notebook and terminal

TLDR: When I write a small script named toy_model.py and attempt to run it from the command line with TLDR:当我编写一个名为 toy_model.py 的小脚本并尝试从命令行运行它时

py toy_model.py

I get an error message complaining about loading tensorflow.我收到一条错误消息,抱怨加载 tensorflow。

However, I am able to use import and use tensorflow in many other settings without any problem, such as但是,我可以在许多其他设置中使用导入和使用 tensorflow 没有任何问题,例如

  • In a jupyter notebook在 jupyter 笔记本中
  • when I import toy_model.py into a jupyter notebook当我将 toy_model.py 导入 jupyter 笔记本时
  • when I use python from the command line当我从命令行使用 python

I have tried many recommended solutions (downloading Spyder in the Anaconda Navigator virtual environment I am using, switching from tensorflow 2.1.0 to tensorflow 2.0.0, downloading microsoft visual studio), and none have been succesful.我已经尝试了很多推荐的解决方案(在我正在使用的 Anaconda Navigator 虚拟环境中下载 Spyder,从 tensorflow 2.1.0 切换到 tensorflow 和没有成功的 Visual Studio 2.0。

I would be grateful for any assistance or insight into this problem, which I will describe in more detail below.对于这个问题的任何帮助或见解,我将不胜感激,我将在下面更详细地描述。


I use Anaconda Navigator to code in Python.我使用 Anaconda Navigator 在 Python 中编码。 In Anaconda Navigator, I prepared an environment with the name updated_tensorflow .在 Anaconda Navigator 中,我准备了一个名为updated_tensorflow的环境。 I used Anaconda's package manager to download tensorflow 2.0.0 and keras 2.3.1 into this environment.我使用 Anaconda 的 package 管理器将 tensorflow 2.0.0 和 keras 2.3.1 下载到此环境中。

I prepared a jupyter notebook named test1.ipynb with the following code:我准备了一个名为test1.ipynb的 jupyter notebook,代码如下:

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import keras
model = tf.keras.Sequential([
    tf.keras.layers.Dense(1, input_shape=(10,))])
model.compile(optimizer='adam',
          loss= 'sparse_categorical_crossentropy', #should be 'sparse_categorical_crossentropy' b/c one-hot encoded
          metrics=['accuracy'])
model.predict([[1,2,3,4,5,6,7,8,9,10], [-1,2,-3,4,-5,6,-7,8,-9,10]])

When I run test1.ipynb from the environment updated_tensorflow , there are no problems.当我从环境updated_tensorflow运行test1.ipynb时,没有问题。

In the terminal I entered the environment updated_tensorflow , and began using python by typing python in the command line.在终端中,我进入环境updated_tensorflow ,并通过在命令行中键入python开始使用 python。 I entered the same code as in test1.ipynb and had no problems.我输入了与test1.ipynb相同的代码,没有任何问题。

I create a file with the name toy_model.py that contained the following code:我创建了一个名为toy_model.py的文件,其中包含以下代码:

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import keras
model = tf.keras.Sequential([
    tf.keras.layers.Dense(1, input_shape=(10,))])
model.compile(optimizer='adam',
          loss= 'sparse_categorical_crossentropy', #should be 'sparse_categorical_crossentropy' b/c one-hot encoded
          metrics=['accuracy'])

Then, I created another jupyter notebook in the same directory as toy_model1.py with the name test2.ipynb and the following code:然后,我在与test2.ipynb toy_model1.py代码如下:

from toy_model1 import *
model.predict([[1,2,3,4,5,6,7,8,9,10], [-1,2,-3,4,-5,6,-7,8,-9,10]])

This cell ran with no problems.这个单元运行没有问题。

Finally, in this same directory, I produced a small file with the name toy_model.py which contained the code最后,在同一个目录中,我生成了一个名为toy_model.py的小文件,其中包含代码

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import keras
model = tf.keras.Sequential([
    tf.keras.layers.Dense(1, input_shape=(10,))])
model.compile(optimizer='adam',
          loss= 'sparse_categorical_crossentropy', #should be 'sparse_categorical_crossentropy' b/c one-hot encoded
          metrics=['accuracy'])
model.predict([[1,2,3,4,5,6,7,8,9,10], [-1,2,-3,4,-5,6,-7,8,-9,10]])

Then in my terminal, still in the environment updated_tensorflow , I moved to the directory containing toy_model.py and attempted to run it with然后在我的终端中,仍然在环境updated_tensorflow中,我移动到包含toy_model.py的目录并尝试运行它

py toy_model.py

I got the following message that indicated I could not import tensorflow:我收到以下消息,表明我无法导入 tensorflow:

Traceback (most recent call last):
  File "C:\Users\me\Anaconda3\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\me\Anaconda3\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\me\Anaconda3\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "C:\Users\me\Anaconda3\Anaconda3\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\me\Anaconda3\Anaconda3\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "toy_model.py", line 3, in <module>
    import tensorflow as tf
  File "C:\Users\me\Anaconda3\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Users\me\Anaconda3\Anaconda3\lib\site-packages\tensorflow\python\__init__.py", line 50, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Users\me\Anaconda3\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 69, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "C:\Users\me\Anaconda3\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\me\Anaconda3\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\me\Anaconda3\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "C:\Users\me\Anaconda3\Anaconda3\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\me\Anaconda3\Anaconda3\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

Some of the advice I have followed in my unsuccessful efforts to fix this problem includes:我在尝试解决此问题时所遵循的一些建议包括:

  • making sure pip and setup tools are updated确保 pip 和设置工具已更新

  • uninstalling and reinstalling tensorflow and keras using pip使用 pip 卸载并重新安装 tensorflow 和 keras

  • uninstalling and reinstalling tensorflow and keras using conda使用 conda 卸载和重新安装 tensorflow 和 keras

  • switching from tensorflow 2.1.0 to tensorflow to 2.0.0从 tensorflow 2.1.0 切换到 tensorflow 到 2.0.0

  • installing tensorflow and keras on my base anaconda environment在我的基础 anaconda 环境上安装 tensorflow 和 keras

  • installing tensorflow and keras on my machine在我的机器上安装 tensorflow 和 keras

  • downloading Spyder in my updated_tensorflow environment在我的updated_tensorflow环境中下载 Spyder

  • downloading Microsoft visual studios None have been successful.下载 Microsoft Visual Studios 都没有成功。

I would be very grateful for any assistance in understanding and fixing whatever error I am making.我将非常感谢在理解和修复我所犯的任何错误方面提供的任何帮助。 It would be so nice if I could use run.py files from my terminal instead of using python exclusively in Jupyter notebooks!如果我可以在终端中使用 run.py 文件,而不是在 Jupyter 笔记本中专门使用 python,那就太好了!

Even a hint as to what the error message means would be helpful: I do not properly understand what a DLL even is.即使是关于错误消息含义的提示也会有所帮助:我没有正确理解 DLL 甚至是什么。

It sounds like, by invoking python , you are not getting the specific python installation that has tensorflow (or tensorflow in that installation is broken).听起来,通过调用python ,您没有获得具有 tensorflow 的特定 python 安装(或 Z2C39BC19B761AC36FEDZ042 安装已损坏)76D146。 By default, invoking python will give you the system default (it's the first one in the environment path, so that's the first hit).默认情况下,调用python将为您提供系统默认值(它是环境路径中的第一个,所以这是第一个命中)。

I would advise identifying exactly which python interpreter your notebook is using and calling that one specifically by saying我建议准确确定您的笔记本正在使用哪个 python 解释器,并通过说

/path/to/notebook/interpreter/python toy_model.py

Try to get the latest supported Microsoft Visual C++尝试获取最新支持的 Microsoft Visual C++

暂无
暂无

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

相关问题 Tensorflow 在 Python 终端中工作,但在笔记本 jupyter 和 Ipython 中均无效 - Tensorflow works in Python terminal but neither in notebook jupyter nor in Ipython 无法在 jupyter 笔记本中导入 tensorflow - can't import tensorflow in jupyter notebook import tensorflow 可以在 jupyter notebook 中使用,但不能在命令行中使用。 使用 conda 安装 tensorflow 和 jupyter notebook - import tensorflow works in jupyter notebook but not from command line. Using conda to install tensorflow and jupyter notebook 即使目录包含在 PATH 中,也无法在不更改目录的情况下从终端运行 Python 脚本 - Can't run Python script from Terminal without changing directories even though directory is included in PATH “未找到导入 tensorflow 模块”仅在 jupyter 笔记本上,但不在 jupyter 实验室或终端上 - "Importing tensorflow module not found" Only on jupyter notebook but not jupyter lab or terminal 如何从终端运行 an.ipynb Jupyter Notebook? - How to run an .ipynb Jupyter Notebook from terminal? 无法在 Jupyter 中导入 tensorflow - Can't import tensorflow in Jupyter 在Jupyter Notebook上导入错误OpenCV(但在Terminal上的Ipython中有效。) - import error OpenCV on Jupyter Notebook (but it works in Ipython on Terminal..) Google Cloud 无法从 Jupyter Notebook 保存 Tensorflow model - Google Cloud can't save Tensorflow model from Jupyter Notebook Tensorflow 2 适用于 Jupyter Notebook 但不适用于 VsCode - Tensorflow 2 works on Jupyter Notebook but not on VsCode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM