No module named tensorflow ошибка

Please help me with this error

I have installed the tensorflow module on my server and below is it’s information

15IT60R19@cpusrv-gpu-109:~$ pip show tensorflow
Name: tensorflow
Version: 1.0.0
Summary: TensorFlow helps the tensors flow
Home-page: http://tensorflow.org/
Author: Google Inc.
Author-email: opensource@google.com
License: Apache 2.0
Location: /home/other/15IT60R19/anaconda2/lib/python2.7/site-packages
Requires: mock, numpy, protobuf, wheel, six

But when I try to import tensorflow I get following error

>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named tensorflow

My python version is as following

Python 2.7.12 |Anaconda 2.5.0 (64-bit)| (default, Jul  2 2016, 17:42:40) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org

I have tried the solutions given in
sol1

Sol2

I do not have sudo access to the server
I can only use pip to install any module

Please help me with this error

I have installed the tensorflow module on my server and below is it’s information

15IT60R19@cpusrv-gpu-109:~$ pip show tensorflow
Name: tensorflow
Version: 1.0.0
Summary: TensorFlow helps the tensors flow
Home-page: http://tensorflow.org/
Author: Google Inc.
Author-email: opensource@google.com
License: Apache 2.0
Location: /home/other/15IT60R19/anaconda2/lib/python2.7/site-packages
Requires: mock, numpy, protobuf, wheel, six

But when I try to import tensorflow I get following error

>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named tensorflow

My python version is as following

Python 2.7.12 |Anaconda 2.5.0 (64-bit)| (default, Jul  2 2016, 17:42:40) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org

I have tried the solutions given in
sol1

Sol2

I do not have sudo access to the server
I can only use pip to install any module

Python is known for its versatile syntax and English-like keywords. With thousands of modules, you can do data visualization, data processing and even deploy machine learning models. There are many known machine learning models published which help you, namely, Keras, Sklearn, Tensorflow, and PyTorch. Although, while using Tensorflow, you can encounter a No Module named Tensorflow error while running your first program.

No Module Named Tensorflow Error is a known error that arises when the Python Environment is unable to fetch TensorFlow files in site-packages. There are two main reasons for this error to appear, either you have not installed the TensorFlow external module or you are working on a different python environment that doesn’t have Tensorflow. There are several easy ways to fix this error which are mentioned later in this post. Let’s understand the root cause of the error before jumping on the solution.

No Module Named Tensorflow Error

When a module is absent from the external site-library of the environment, the Python interpreter throws ModuleNotFoundError No Module Named Tensorflow. This error arises most of the time on low-end devices because TensorFlow requires proper setup of the c++ path and other requirements. Most of the time, this error is solved by using the pip install method. But if you have multiple Python versions installed, then you’ll definitely face this error.

Why do I get No Module Named Tensorflow Error?

There is no other reason for the No Module Named Tensorflow error other than missing module files. The main problem arises when you’re using multiple python versions and their virtual environment. Keep in mind that, Anaconda, PyCharm, Jupyter, and Spyder have their own virtual environment and it’s tricky to install modules in that environment.

Causes of No Module Named Tensorflow Error

There are some known causes of this ModuleNotFoundError. As Python lets you handle these errors easily, you can debug them quickly. Following are the cause of the No Module Tensorflow error –

Module Not Installed

If you haven’t installed TensorFlow yet and tried importing TensorFlow in code, then it’ll throw this error. Modules are managed by ‘pip’ which is a package management system for your python. Most of the time, the users forget to tick Add Python to the PATH option while installing it. This creates problems in managing the modules.

Supporting Module Not Installed

Tensorflow has many other supporting modules like numpy, scipy, jupyter, matplotlib, pillow, and scikit-learn. If any of these modules is absent, then it’ll throw an error. Make sure that these modules exist in your library.

Moreover, there are other supporting TensorFlow modules like, tensorflow-addons, tensorflow.contrib which might be absent from your library leading to this error.

Tip: To check which libraries are installed in your environment, enter pip list on your console.

Working on Different Virtual Environment

Virtual Environment: Method by which you isolate your working Python environment from the globally installed Python. This environment has its own installation directories and doesn’t share the libraries from globally installed Python.

Many of the code editors in Windows come with their own virtual environment. Each of these environments acts independently to global python installation and is started with blank external modules. Many times, you install a module but it’s installed on global python, not the python from your virtual environment. This can lead to ModuleNotFoundError No Module named Tensorflow in the code execution.

Code editors namely, Anaconda, Jupyter, and Spyder have their own virtual environment. If you have a similar case, head over to the corresponding solution for each code editor.

Solutions for No Module Named Tensorflow

Following are the solutions for this error in each code editor and OS –

Windows

No Module Tensorflow Windows

In Windows, the path-related issues harras the programmers all the time. As you have limited functionality over the terminal, you’re constantly facing with ModuleNotFoundError error. To install TensorFlow in your Windows, make sure you follow these steps –

  1. Uninstall existing python versions to avoid any conflicts.
  2. Go to Python.org and install the Python setup. Make sure you install the 64-bit version. Unfortunately, Tensorflow is not supported in 32 bit systems.
  3. Open the installer and select the “Add Python x.x to your PATH” option. This will ensure, that python executes from the path.
  4. After installing, open the command terminal or PowerShell and enter the command pip install tensorflow in it.
  5. Wait for it to finish the installation and run your python file by command python file.py

Linux

In Linux, it’s relatively easier to install TensorFlow. First of all, check if you are working on a virtual environment by a command which python. This command will return the path of python which you’re going to execute. If you are in a virtual environment, either leave the environment directory or enter the command deactivate to deactivate the virtual environment.

Note: Please do not try to uninstall python in Linux as it’ll interfere with your GDM (Graphical Display Manager). Instead, install a new version and then create your own virtual environment.

Follow these steps to install Tensorflow in Linux –

sudo pip3 install tensorflow

Mac

In Mac, No Module named Tensorflow is a persistent error because of environment errors. If you are working on your virtual environment, you need to deactivate and activate it again. Then use the pip list command to check if the TensorFlow module exists in your library. If not, then use the pip3 install tensorflow to install TensorFlow.

Anaconda

No Module Named Tensorflow in Anaconda

If you’re using Anaconda and you face no module named Tensorflow error, then you probably haven’t installed TensorFlow in the conda environment. As anaconda has a different environment than your default python environment, you need to install TensorFlow in it. To do it follow these steps –

  1. Open Anaconda Prompt on your computer.
  2. Enter the command conda install tensorflow in it.
  3. Wait for the installation to complete and restart the conda shell and run your program.

Jupyter

If you’ve installed Juptyter Notebook from Anaconda, it’ll use a conda environment. By default, the libraries in this environment need to be installed via command. To do the same, open your Conda Prompt and enter the command conda install tensorflow. This will ensure that your Juptyer Notebook has TensorFlow in it.

If your Jupyter is not installed via Anaconda, then use the pip install tensorflow to install the TensorFlow module. This will resolve the error ModuleNotFoundError No module named Tensorflow instantly.

Spyder

Spyder is installed via Anaconda which operates an Anaconda environment. Simply use the command conda install tensorflow in your Anaconda Prompt to install TensorFlow.

PyCharm

PyCharm is a special application that operates in its own virtual environment. Due to the unavailability of the TensorFlow error, you can face the no module found error. Follow these steps to install TensorFlow –

  1. Press Settings and select the Project Interpreter tab under projects.
  2. Now enter Tensorflow in the box and install it.
  3. After installing the package, your error will be resolved.

Supporting ModuleNotFoundError for Tensorflow

Tensorflow has many addons which come in handy to avoid writing long code. These addons and contributions are added separately in other packages and combine with the original TensorFlow module. Following are the examples of ModuleNotFoundError –

No module named ‘tensorflow.contrib’

Unfortunately, the contrib module in TensorFlow is not included in version 2.0. If you still want to use the contrib module, you’ll have to install the previous version of TensorFlow. Follow these steps –

pip uninstall tensorflow
pip install tensorflow==1.13.2

No module named ‘tensorflow_addons’

Use pip install tensorflow-addons to install the addons for TensorFlow.

No Module Named Tensorflow Still Not Resolved?

If you’ve tried all the methods and were still not able to solve the issue then, there might be some hardware limitations. Tensorflow requires Python 3.5-3.7, 64-bit system, and pip>=19.0. If you’re unable to fulfill these hardware + software requirements, then don’t worry, we still have a solution for you!

Google released a free product named ‘Colab‘ in 2018. Colab allows you to run and test machine learning models online. To explain more, it’s a replica of the jupyter notebook with all modules installed. To use Tensorflow in Google Colab follow these steps –

  1. Sign in to your Google account.
  2. Open Colab in your browser.
  3. Create a new Jupyter Notebook. (This notebook will be saved in your google drive).
  4. Type your code and run it.

Using Tensorflow in Colab

Colab has many libraries like TensorFlow, numpy, pandas, etc pre-installed in its shell. Make sure you make good use of it.

Tip: Do not use Colab to store/process peer-to-peer files. This may result in a ban!

See Also

Conclusion

Tensor flow has a flexible architecture. The easy deployment of the code makes it special in nature. However, we have to be very careful before using it. Any small syntax error can result in incorrect importing of the library.

Couldn’t reproduce the error. It’s really something on your side.

C:\WINDOWS\system32>activate tensorflow

(tensorflow) C:\WINDOWS\system32>pip install --ignore-installed tensorflow
Collecting tensorflow
  Using cached tensorflow-0.12.1-cp35-cp35m-win_amd64.whl
Collecting numpy>=1.11.0 (from tensorflow)
  Downloading numpy-1.11.3-cp35-none-win_amd64.whl (7.6MB)
    100% |################################| 7.6MB 123kB/s
Collecting six>=1.10.0 (from tensorflow)
  Using cached six-1.10.0-py2.py3-none-any.whl
Collecting protobuf>=3.1.0 (from tensorflow)
  Downloading protobuf-3.1.0.post1-py2.py3-none-any.whl (347kB)
    100% |################################| 348kB 907kB/s
Collecting wheel>=0.26 (from tensorflow)
  Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
    100% |################################| 71kB 1.1MB/s
Collecting setuptools (from protobuf>=3.1.0->tensorflow)
  Downloading setuptools-32.3.1-py2.py3-none-any.whl (479kB)
    100% |################################| 481kB 415kB/s
Installing collected packages: numpy, six, setuptools, protobuf, wheel, tensorflow
Successfully installed numpy-1.11.3 protobuf-3.1.0.post1 setuptools-32.3.1 six-1.10.0 tensorflow-0.12.1 wheel-0.29.0

(tensorflow) C:\WINDOWS\system32>python
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, testing TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
b'Hello, testing TensorFlow!'

In this Python tutorial, we will discuss the error “import error no module named TensorFlow. Here we’ll cover the reason related to this error using Python. And we’ll also cover the following topics:

  • Import error no module named tensorflow.compat.v1
  • Import error no module named ‘tensorflow.contrib’
  • Import error no module named tensorflow_hub
  • Import error no module named ‘tensorflow addons’
  • Import error no module named ‘tensorflow.python.eager’
  • In this section, we will discuss the error import error no module named TensorFlow in Python.
  • Basically, this error message comes when we do not import the TensorFlow library or we can say the TensorFlow library is not installed in our system.
  • If you have not installed the TensorFlow library in your system and still want to try importing the TensorFlow library in your program then it will raise an error message ‘no module named TensorFlow.

Example:

import tensorflow as tf

tens1 = tf.Variable([[67, 89],[23, 45]])
tens2 = tf.Variable([[94, 117],[189, 210]])
new_output= tf.add(tens1,tens2)
print(new_output)

In the above code, we have used the tf.add() function and within this function, we assigned the given tensors ‘tens1’ and ‘tens2’ as an argument.

Here is the Screenshot of the following given code.

Python import error no module named TensorFlow
import error no module named TensorFlow

Now let’s see the solution for this error:

Visual Code Studio

If you have installed Visual code Studio then it will use a pip environment and if you want to import some needed libraries then you have to install via command.

  • install TensorFlow using pip command

Syntax:

Here is the Syntax of the installation of the TensorFlow library

pip install TensorFlow

This Syntax will help you to resolve the error ‘no Module named TensorFlow’

Jupyter

If you have installed Jupyter notebook then use the Conda environment. To get detailed information regarding how to install the TensorFlow library in the Conda environment. You can refer to our detailed article TensorFlow in Python.

Also, check: TensorFlow Sparse Tensor

Import error no module named ‘tensorflow.contrib’

  • Here we are going to discuss the error import error no module named ‘TensorFlow.contrib’.
  • Basically, the contrib module is not available in TensorFlow version 2.0. This module contains contributed code and it is also a volatile code.
  • The possible reason behind this error is when the interpreter cannot locate the contrib ‘module’ in the library.
  • The latest version of TensorFlow 2.x has removed the session and contrib module from the TensorFlow library.

Reason: module ‘contrib’ is not working in version 2.x

Example:

import tensorflow.contrib

Here is the Screenshot of the following given code

import error no module named 'tensorflow.contrib'
import error no module named ‘TensorFlow.contrib’

In the following given code, we have just imported the ‘TensorFlow.contrib’ module but when we executed this command it displays the No module named ‘TensorFlow.contrib’. The possible reason is this module does not work in the latest version of TensorFlow 2.x.

Let’s discuss the solution to this error

  • First, uninstall the already installed version, if it is the latest version then reinstall it with the older version. To uninstall the tensorflow 2.x you can easily use the command pip uninstall TensorFlow.
  • Now to install older version of TensorFlow 1.x you can use below command
pip install tensorflow==1.15.0

Read: Python TensorFlow reduce_sum

Import error no module named tensorflow.compat.v1

  • In this section, we are going to discuss the error no module named tensorflow.compat.v1.
  • The possible reason behind this error is that compat.v1 modules are deprecated in Tensorflow version 1.12.0. This module will help the user to write the code in both TensorFlow versions.
  • This module does not support the 1.12 version, if you want to execute this module on your system then you have to install the latest or above the 1.12 version.

Example:

import error no module named tensorflow compat v1 in Python
import error no module named TensorFlow compat v1 in Python

Now we are going to use the updated version of TensorFlow for importing the TensorFlow.compat.v1 module in Python

To install the new version of TensorFlow in Conda prompt you can use the below command

conda install tensorflow==2.6
Solution of no module named tensorflow compat v1 in Python
Solution of no module named TensorFlow compat v1 in Python

As you can see in the Screenshot we have checked the version of TensorFlow it displays 2.6 latest version and also successfully imported the TensorFlow.compat.v1.

Also, check: Module ‘TensorFlow’ has no attribute ‘session’

Import error no module named ‘tensorflow_hub’

  • In this Program, we will discuss the error no module named ‘tensorflow_hub’ in Python.
  • Basically, this error message comes when the module ‘tensorflow_hub’ is not available in the TensorFlow library package.

Example:

Python import error no module named tensorflow hub
Python import error no module named TensorFlow hub

As you can see in the Screenshot it displays no module name ‘tensorflow_hub’ the reason behind this is we are using the older version of TensorFlow 1.14. In this version, the tensorflow_hub module is not working.

Let’s have a look at the Solution to this error

To solve this error you have to install the latest version of TensorFlow in your system. By default 2.7 is a stable version and to install the updated version of TensorFlow, you can use the below command. This command is used for conda prompt users.

conda install tensorflow==2.7

Screenshot

Solution of import error no module named tensorflow hub
Solution of import error no module named TensorFlow hub

In this Screenshot, you can see that we have installed the latest version of TensorFlow 2.7 and then import the tensorflow_hub module.

Read: TensorFlow Tensor to numpy

Import error no module named ‘tensorflow addons’

  • In this section, we will discuss the error no module named ‘tensorflow addons’ in Python.
  • Basically, the addon module is available in the latest TensorFlow version 2.7 but we have to update the ‘TensorFlow addons’ module. This module is not available in core Tensorflow and it also supports optimizers, metrics, and losses.

Screenshot

import error no module named tensorflow addons
import error no module named TensorFlow addons

As you can see in the output, it displays no module name ‘tensorflow_addons’. And the possible reason is that the ‘tensorflow_addons‘ module is available in TensorFlow latest version 2.7. But we have to update the ‘tensorflow_addons’ module.

Let’s have a look at the Solution to this error

To solve this error you have to install the older version of TensorFlow in your system. By default 2.7 is a stable version but this module only does not work in the latest version 2.x.

Output:

Solution of import error no module named tensorflow addons
Solution of import error no module named TensorFlow addons

To upgrade the ‘TensorFlow addons’ module you can easily use the below command

pip install tensorflow-addons

In the above code, we have imported the tensorflow_addons() module after upgrading the module.

Read: TensorFlow global average pooling

Import error no module named ‘tensorflow.python.eager’

  • Here we are going to discuss the error no module named ‘tensorflow.python.eager’ in Python.
  • When I was trying to import the ‘TensorFlow.python.eager’ module this error comes out in output. The reason behind that is the version of the TensorFlow library.

Let’s have a look at the Solution to this error

In this example, we have just imported the TensorFlow library and then checked the version by using the tf.__version__ command. After that, we have imported the ‘tensorflow.python.eager’ module. Once you will execute this code the output displays the module has been successfully imported.

Example:

import error no module named tensorflow python eager
import error no module named tensorflow python eager

Also, take a look at some more Python TensorFlow tutorials.

  • Binary Cross Entropy TensorFlow
  • Gradient descent optimizer TensorFlow
  • Python TensorFlow Placeholder
  • Convert list to tensor TensorFlow
  • Module ‘tensorflow’ has no attribute ‘div’
  • Module ‘tensorflow’ has no attribute ‘sparse_placeholder’

So, in this tutorial, we have solved the error “import error no module named TensorFlow“. Here we’ll cover the reason related to this error using Python. And we’ll also cover the following topics:

  • import error no module named tensorflow.compat.v1
  • import error no module named ‘tensorflow.contrib’
  • import error no module named tensorflow_hub
  • import error no module named ‘tensorflow addons’
  • import error no module named ‘tensorflow.python.eager’

Fewlines4Biju Bijay

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

Понравилась статья? Поделить с друзьями:
  • Not permissible ошибка порше кайен
  • Not page fail not area ошибка виндовс
  • Not null constraint failed ошибка
  • Nox player ошибка при синтаксическом анализе пакета
  • Not mpisp mode 1b ssd ошибка