How to Fix ModuleNotFoundError: No Module Named Tensorflow in Python

Salman Mehmood Feb 16, 2024
How to Fix ModuleNotFoundError: No Module Named Tensorflow in Python

We will learn how to install TensorFlow and try several ways to install it; then, we will also learn how to install TensorFlow in a separate conda environment in Python.

Use the Correct Way to Install TensorFlow in Python

TensorFlow does not come automatically with Anaconda distribution, so we have to install it separately. Before installing, if we write the command to import tensorflow, we will get an error message like the one below.

import tensorflow as tf

Output:

ModuleNotFoundError: No module named 'tensorflow'

Now we will show you how to install this; you can download the Anaconda distribution from here for Windows 64-bit, and after this download, we need to install that, and the installation instruction is also given in this link.

There are two methods for installing TensorFlow:

  1. Through the command prompt, we will give this command, and the TensorFlow will be installed.
conda install -c tensorflow-gpu

Or

conda create --name tensorflow python=3.5
activate tensorflow
  1. Another way to install is to go to Anaconda navigator and click on the environment; then, we will have a based environment that is the root environment. If we want to install TensorFlow, we must create another environment.

We will click on the Environments tab on the right side, and after opening the Environments tab, we need to click on the Create button; then, we need to give a name to the new environment.

It will automatically pick the Python version; otherwise, we can select this via a drop-down and click on the Create button.

Anaconda navigator

It will take some time if we go to the new environment, but we can see we have installed all the basic packages. For TensorFlow, we have to select the Not installed option, and after selecting this option, we need to search “tensorflow” in the search box.

After clicking on the correct module, we click Apply, and we will also install the keras library in this TensorFlow environment. It will take some time, and sixty software packages will be installed to run the tensorflow library.

Search tensorflow packages

If we go there and search for TensorFlow, we will get this; after selecting the new environment, we have to go home and install the Jupyter notebook, then we can launch from here. After installation, we will launch the Jupyter notebook and try to import TensorFlow.

If you get any errors while importing TensorFlow, do not worry; we have another way to install TensorFlow. First, we have to uninstall TensorFlow from the Anaconda environment because it is useless if it does not work.

We need to go back to the Anaconda navigator and find the TensorFlow package and left-double click on it; the remove option will appear to click on it and apply for this process. After the uninstallation, we need to open the command prompt and run the following command.

pip install tensorflow

After installation, we will run a Jupyter notebook from the local environment because we have installed TensorFlow in the local environment. Now, if we try to import TensorFlow, this time, TensorFlow has been imported, and no error message has been shown.

No error shown after importing

If any method does not work, follow this link, where you will find other ways to install TensorFlow.

Salman Mehmood avatar Salman Mehmood avatar

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.

LinkedIn

Related Article - Python Error