[Solved] No Module Named Tensorflow Error

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.

What is No Module Named Tensorflow Error?

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.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Benoit Brummer (Trougnouf)
Benoit Brummer (Trougnouf)
2 years ago

Deleting python on Ubuntu Linux will wreck a system. Luckily I noticed someone following your suicidal instructions on the server we use and I reinstalled everything before the session ended.