How To Solve The Error “ModuleNotFoundError: No Module Named ‘Tensorflow’” In Python

ModuleNotFoundError: No module named ‘tensorflow’ in python

The “ModuleNotFoundError: No module named ‘tensorflow’” error in Python shows that Python cannot find the module ‘tensorflow’ that is imported in your code or some packages your code imports. This tutorial will show you how to fix the error by installing ‘tensorflow’ in your environment.

The reason for the error “ModuleNotFoundError: No module named ‘tensorflow’” in Python

When you run your Python code, the Python runtime will look for all the packages or modules you import to your code and their dependencies. If the runtime cannot find a module, it will throw a ModuleNotFoundError. The error “ModuleNotFoundError: No module named ‘tensorflow’” show that Python cannot find ‘tensorflow’ in your environment, so you need to install it to run your code correctly. Here I have an example code that imports ‘tensorflow’ and prints its version. You will get the error if you run this code in a Python environment without ‘tensorflow’ installed.

Example code:

import tensorflow as tf
print("Tensorflow version:", tf.__version__)

Output:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import tensorflow as tf
ModuleNotFoundError: No module named 'tensorflow'

How to solve the error?

Method 1: Install tensorflow directly with pip

Pip is the package installer for Python that comes with Python when you install Python on your machine. You can check the version of pip by the following command in your terminal of Linux/MacOS or the Command prompt of Windows. 

pip --version

Output:

pip 21.1.3 from <the path where pip installed on your machine>

To install ‘tensorflow’ using pip, you can easily run this command.

pip install tensorflow

You can also specify the ‘tensorflow’ version that you want to install like this.

pip install tensorflow==2.10.0

After successfully installing ‘tensorflow’, you can use it in your code as you want.

Example code:

import tensorflow as tf
print("Tensorflow version:", tf.__version__)

Output:

Tensorflow version: 2.10.0

Method 2: Install tensorflow in a python virtual environment

It is recommended to install the packages that you need for different projects in different environments because you may want to use different versions of a package in your projects. Using different environments helps to keep the packages not conflict with each other. You can create and activate a virtual environment by the following steps.

  • Step 1: Install virtualenv package
pip install virtualenv
  • Step 2: Create the environment folder where the packages will be installed
python -m venv env
  • Step 3: Activate the environment

On Linux/MacOS:

source env/bin/activate

On Windows:

cd env/Scripts
activate.bat

Step 2 will create an environment folder name env in the directory where you run the command. After activating your environment, you can normally install ‘tensorflow’ using the pip command: 

pip install tensorflow

Method 3: Install tensorflow using conda

Another way to create python environments is using conda. You can download and install the conda by following this instruction. After installing conda, you can create a new environment by the command:

conda create -n myenv python

Then, you can activate your environment:

conda activate myenv

Finally, you can install ‘tensorflow’ using pip or conda if you want.

conda install -c conda-forge tensorflow

Summary

In this tutorial, I’ve showed you how to solve the error  “ModuleNotFoundError: No module named ‘tensorflow’” In Python. You can install ‘tensorflow’ directly with pip, but it is recommended to create an environment by the Python virtualenv or conda.

Maybe you are interested in similar errors:

Leave a Reply

Your email address will not be published. Required fields are marked *