Solve tensorflow error – cannot import name pywrap_tensorflow

cannot import name pywrap_tensorflow is an error which occurs when your Python path loads tensorflow libraries from the source directory.

In this article I am going to explain why the error is happening and how to solve it without getting the error again. Also we are going to check out other solutions that may work for your particular case.

Explaining the Error : cannot import name pywrap_tensorflow

Before we solve this error, we should understand that this error is a Tensorflow error first and not only a pywrap error. Bellow is an example of the error message.

                                                                       #
/git/tensorflow/tensorflow/__init__.py in <module>()
     21 from __future__ import print_function
---> 23 from tensorflow.python import *
/git/tensorflow/tensorflow/python/__init__.py in <module>()
     43 _default_dlopen_flags = sys.getdlopenflags()
---> 45 from tensorflow.python import pywrap_tensorflow
     46 sys.setdlopenflags(_default_dlopen_flags)
ImportError: cannot import name pywrap_tensorflow
                                                                       #

To solve the problem above, I have a couple of solutions which have worked for me, bellow is a detailed explanation of the solutions.

Solution 1 : install TensorFlow in a Python 2.7 virtual environment (venv)

This is the solution that has worked for me after a lot of trial and error.

First, Create a virtual env. First launch the following command.

                                                                       #
cd /home/victoria/anaconda3/envs
                                                                       #

Then, launch the following command.

                                                                       #
conda create -n tf-env python=2.7 anaconda
                                                                       #

Now, let us activate the virtual environment. By using the command bellow. Then add to ~/.bashrc as an alias.

                                                                       #
source activate tf-env
                                                                       #
                                                                       #
alias tf='echo "  [TensorFlow in Anaconda Python 2.7 venv (source activate tf]" && source activate tf-env'
                                                                       #

Finally, Install TensorFlow in the virtual env. After using cd of course.

                                                                       #
conda install -c conda-forge tensorflow
                                                                       #

If everything works, great. If not, follow the solution bellow. It may help you.

Solution 2 : get out of /git/tensorflow

Instead of loading the tensorflow libraries from the source directory, you should be loading the version that you have installed. 

The fix is to cd out of the path bellow before starting ipython or python.

                                                                       #
/git/tensorflow
                                                                       #

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

This is the end of our article, I hope this helped you solve the error or at least showed you the way. You can donate to our Kofi account but you do not Have to. Keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/