Attributeerror: module ‘tensorflow’ has no attribute ‘session’

The attributeerror: module ‘tensorflow’ has no attribute ‘session’ is an error message that occurs when you are using a TensorFlow library to create a session.

However, the module cannot find the attribute ‘session’ because tf.Session() is incompatible with version 2 of TensorFlow.

It happens when you are using a newer version of TensorFlow that has changed, deprecated, or removed the attribute ‘session. In this article, we will show you what this error means and provide solutions to help you fix it.

What is “attributeerror: module ‘tensorflow'” has no attribute ‘session’?

AttributeError: module 'tensorflow' has no attribute 'session'

The error message attributeerror: module tensorflow has no attribute session usually happens when you are trying to use the deprecated session in TensorFlow version 1. In version 2 of TensorFlow, the session is no longer explicitly useful.

Aside from that, eager execution mode has been enabled by default, and you don’t have to create a session object directly. Instead, you can use operations directly by calling them as functions.

In addition to that, this error is caused by the incompatibility of TensorFlow module 2.x. To fix this error, you can remove the session code and use the new TensorFlow 2.x APIs instead.

How to use ‘session’ in TensorFlow 2.0?

This is an example of running a program in version 1 of TensorFlow:

import tensorflow as tf

msg = tf.constant('This will result an error!')

sess = tf.Session()

print(sess.run(msg))

As a result of the code above, it will throw an : AttributeError: module 'tensorflow' has no attribute 'Session'

Solution:

The TensorFlow 2.0 offers the option to disable eager execution by default when running older code for compatibility and to execute TensorFlow 1.x code. However, updating your code to TensorFlow 2.0 is advised.

This example shows a simple running session in version 2 of tensorflow:

import tensorflow as tf

tf.compat.v1.disable_eager_execution()

msg = tf.constant('Hi, Thank you')

sess = tf.compat.v1.Session()

print(sess.run(msg ))

How run Tensorflow in Pycharm?

Time needed: 2 minutes

  1. Open your Pycharm application.

  2. Go to File tab and select “settings.”

    Go to File tab

  3. Click “Project:pythonProject” and select “Python Interpreter.”

    Click project

  4. Click the (+) sign.

    Click the (+) sign

  5. Input “tensorflow” in the search box.

    After “inputing tensorflow” click install package if you haven’t installed it yet.

    Input "tensorflow" in the search box.
    After the installation, you can use and run the code without encountering any errors.

Solutions to fix the error “attributeerror: module ‘tensorflow'” has no attribute ‘session’

Here are the solutions that you may use to resolve the error attribute error module tensorflow has no attribute session that you currently facing. Open your command prompt or terminal.

1. Upgrade your code

To fix this error, you have to upgrade your code to the latest version of TensorFlow. You have to change every occurrence of sess.Session.run by calling the Python function.

In newer versions of TensorFlow, you can create a session using the tf.compat.v1.Session() method instead of tf.Session().

Alternatively, you can use the tf.compat.v1.disable_v2_behavior() method to enable compatibility with older versions of TensorFlow that use the tf.Session() method.

2. Use Tensorflow version 1 syntax

You can use this syntax to import, since it is not compatible with TensorFlow 2.x. Luckily, if you use this command, it supports TensorFlow 1.x syntax.

import tensorflow.compat.v1 as tf 

 tf.disable_v2_behavior()

3. Downgrade Tensorflow version

You can easily downgrade the tensorflow version to 1. x using the following command:

pip install tensorflow==1.15.5

However, using the given command above is not a recommended solution because it might introduce incompatibilities with your code. Hence, if you are sure that you are just using the 1.x code base and only the installation version, you can use that command to downgrade the tensorflow.

4. Complete the program code conversion from 1. x to 2. x

If you already installed the TensorFlow 2.0 x in your system and wanted to use TensorFlow version 1.x as well as the supporting code. Therefore, use the following syntax if you encounter this attribute problem.

tf_upgrade_v2 \
  --intree code/ \
  --outtree code_v2/ \
  --reportfile text.txt

Related Articles for Python Errors

Conclusion

This article provides solutions for the attributeerror: module ‘tensorflow’ has no attribute ‘session’,” which is a big help in solving the problem you are currently facing.

Thank you very much for reading until the end of this article. Just in case you have more questions or inquiries, feel free to comment.

Leave a Comment