0

import tensorflow as tf

I get this message in Anaconda interpreter... can somebody help?

Thanks!


ImportError                               Traceback (most recent call last)
~\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>
     57 
---> 58   from tensorflow.python.pywrap_tensorflow_internal import *
     59   from tensorflow.python.pywrap_tensorflow_internal import __version__

~\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in <module>
     27             return _mod
---> 28     _pywrap_tensorflow_internal = swig_import_helper()
     29     del swig_import_helper

~\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in swig_import_helper()
     23             try:
---> 24                 _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
     25             finally:

~\anaconda3\lib\imp.py in load_module(name, file, filename, details)
    241         else:
--> 242             return load_dynamic(name, filename, file)
    243     elif type_ == PKG_DIRECTORY:

~\anaconda3\lib\imp.py in load_dynamic(name, path, file)
    341             name=name, loader=loader, origin=path)
--> 342         return _load(spec)
    343 

ImportError: DLL load failed: Eine DLL-Initialisierungsroutine ist fehlgeschlagen.

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-1-64156d691fe5> in <module>
----> 1 import tensorflow as tf

~\anaconda3\lib\site-packages\tensorflow\__init__.py in <module>
     22 
     23 # pylint: disable=g-bad-import-order
---> 24 from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
     25 
     26 from tensorflow._api.v1 import app

~\anaconda3\lib\site-packages\tensorflow\python\__init__.py in <module>
     47 import numpy as np
     48 
---> 49 from tensorflow.python import pywrap_tensorflow
     50 
     51 # Protocol buffers

~\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>
     72 for some common reasons and solutions.  Include the entire stack trace
     73 above this error message when asking for help.""" % traceback.format_exc()
---> 74   raise ImportError(msg)
     75 
     76 # pylint: enable=wildcard-import,g-import-not-at-top,unused-import,line-too-long

ImportError: Traceback (most recent call last):
  File "C:\Users\MIGUEL\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\MIGUEL\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\MIGUEL\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\MIGUEL\anaconda3\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\MIGUEL\anaconda3\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: Eine DLL-Initialisierungsroutine ist fehlgeschlagen.


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.
AMC
  • 2,642
  • 7
  • 13
  • 35
Miguel Gonzalez
  • 398
  • 1
  • 12
  • Can you share some information on your environment? How did you install the package, what other packages do you have, etc. – AMC Mar 26 '20 at 19:43
  • I saw your other post, are you using the base environment for development?? – AMC Mar 30 '20 at 23:15
  • I just uninstalled anaconda and installed everything again in its last version and it worked. Thanks. – Miguel Gonzalez Mar 30 '20 at 23:23
  • Are you using the base environment for everything this time? – AMC Mar 30 '20 at 23:40
  • Yes I was using always the base environment. The problem was that some packets were not updated, I guess. Tensorflow works now, even though it is reporting a big warning: C:\Users\MIGUEL\anaconda3\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint8 = np.dtype([("qint8", np.int8, 1)]) C:\Users\MIGUEL\anaconda3\lib\sit bla bla bla – Miguel Gonzalez Mar 31 '20 at 15:15
  • _Yes I was using always the base environment._ Are you still doing it now, though? – AMC Mar 31 '20 at 19:56
  • yes I am still using the base environment. Seems to work ok – Miguel Gonzalez Apr 01 '20 at 01:45
  • 1
    Don’t. Nothing good can come from it, and it defeats the point of using virtual environments.... Especially considering how easy it is to create environments, I see no reason not to. – AMC Apr 01 '20 at 02:41
  • What do you mean nothing good can come from it? – Miguel Gonzalez Apr 02 '20 at 02:23
  • 1
    Hi it was finally solved by using a tensorflow environment. The base environment leads to problems when importing tensorflow and its libraries. – Miguel Gonzalez May 05 '20 at 00:30
  • _The base environment leads to problems when importing tensorflow and its libraries._ Using the base environment will lead to issues for all kinds of libraries lol, that's the point I was trying to make in my previous comments. Again, don't touch the base environment, if you have then reset the environment or reinstall Conda. – AMC May 05 '20 at 22:38

2 Answers2

2

Specifying the Answer in this (Answer) section even though it is present in the Comments section for the benefit of the community.

Issue is resolved by Installing Tensorflow in a New Virtual Environment.

Using Virtual Environments has advantages like

  • We can maintain multiple versions of Tensorflow in multiple Virtual Environments with each Virtual Environment comprising each version like 1.14, 1.15, 2.0, 2.1, 2.2,etc..
  • We can use different Python Versions (2.x, 3.6, 3.7) in each Virtual Environment
  • If we want to modify the source code of any of the Tensorflow API, we can do it within our Virtual Environment, without impacting its functionality in other Virtual Environments.

Steps for Creating a New Virtual Environment and installing Tensorflow in Anaconda, for different Operating Systems, is shown below:

# Create a New Virtual Environment
conda create --name TF_2_VE

# When conda asks you to proceed, type y:
proceed ([y]/n)?

# Activate the Virtual Environment. Conda Version > 4.6 
conda activate TF_2_VE

# Activating Virtual Environment, Conda Version < 4.6 and Windows OS
activate TF_2_VE

# Activating Virtual Environment, Conda Version < 4.6 and Linux and Mac OS
source activate TF_2_VE


# Install the TF Version you need
conda install tensorflow

Hope this information helps. Happy Learning!

  • 1
    This answer is specific for linux while OP is using windows – Psychotechnopath May 05 '20 at 08:31
  • Updated the Answer and made it more generic for different Operating Systems. Thank you for the feedback. –  May 05 '20 at 09:20
  • Why install TensorFlow using, pip instead of Conda?? – AMC May 05 '20 at 22:39
  • @AMC, Replaced `pip install tensorflow==2.1` with `conda install tensorflow==2.1`. –  May 06 '20 at 06:00
  • Why version 2.1? Is that what OP was trying to install? – AMC May 06 '20 at 21:10
  • @AMC, I thought if we don't specify the version, it will install Nightly version. But I checked it and that is not the case. Removed the version from the answer. Thank you for the feedback. –  May 07 '20 at 01:55
  • This is what I did in an ubuntu OS: https://medium.com/@nrk25693/how-to-add-your-conda-environment-to-your-jupyter-notebook-in-just-4-steps-abeab8b8d084 – Miguel Gonzalez May 08 '20 at 01:43
0

To install the current release of CPU-only TensorFlow, recommended for beginners:

conda create -n tf tensorflow
conda activate tf

if you already have cuda software

conda create -n tf-gpu tensorflow-gpu
conda activate tf-gpu
Welcome_back
  • 1,245
  • 12
  • 18
  • There is no need to install CUDA-software anymore, a conda install tensorflow-gpu comes with a pre-packed version of the CUDA toolkit and CUDNN Which resides in the specific environment (See this question: https://stackoverflow.com/questions/61533291/is-it-still-necessary-to-install-cuda-before-using-the-conda-tensorflow-gpu-pack) – Psychotechnopath May 05 '20 at 08:34