Home:ALL Converter>Tensorflow import in VSCode fails

Tensorflow import in VSCode fails

Ask Time:2020-12-28T02:42:58         Author:pancakeNbacon

Json Formatter

I'm having trouble to import TensorFlow in my python application, BUT only

  • if I'm running my application in VSCode (via the debugger) OR
  • if I'm running it from the Terminal WITHIN VSCode. Everything is working fine if I'm running the application from the Terminal outside of VSCode.

I'm running VSCode on macOS Big Sur Version 11.1 (M1 chipset). I have python 3.8.2 and TensorFlow installed in a virtual environment.

Here's the steps to reproduce the Error. From the Terminal outside of VSCode I run

  1. source env/bin/activate to activate the virtual environment

  2. python to launch python. Output to the terminal (as expected): Python 3.8.2 (default, Nov 4 2020, 21:23:28) [...]

  3. import tensorflow as tf

  4. print(tf.__version__) This prints "2.4.0-rc0" to the terminal (as expected).

Now, if I repeat the exact same steps 1 and 2 in the built in VSCode terminal, I get the exact same output in 2. However, if I run command 3 and try to import tensorflow, the following error message shows up:

Traceback (most recent call last):
  File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
    from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: dlopen(/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image found.  Did find:
        /Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
        /Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/__init__.py", line 39, in <module>
    from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
  File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 83, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
    from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: dlopen(/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image found.  Did find:
        /Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
        /Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture


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.

It seems like the Terminal within VSCode is not using the same site-packages as the Terminal outside of VSCode, however, running print(sys.path) gives the same result.

The same problem happens if I'm trying to run my application within VSCode whereas it is working if I'm running it from the Terminal.

Any advice is greatly appreciated.

Author:pancakeNbacon,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65469241/tensorflow-import-in-vscode-fails
The Pilot Dude :

Try Python 3.7. Many people have complained tensorflow not working on 3.8, but working on 3.7. Also, try downloading it from the google tensorflow web page.",
2020-12-27T21:18:10
Ryan :

Piggy-backing off of Jay Mody in the comments above. I would first check that the shells you use in VS code is the same as the shell you are using in the non-VS code terminal.\nTry the following shell commands:\necho $SHELL\n\nIf you get the same output, then I would suggest making sure the exact same python executables are being used. Try typing this in both of the shells:\nwhich python\n",
2020-12-27T22:31:58
Molly Wang-MSFT :

In VS Code, open an integrated Terminal, after activating the environment, run\npip show tensorflow\n\nto check if the module exists in the current environment. If not, reinstall it.",
2020-12-28T05:40:05
yy