Shrinidhi Goswami
Shrinidhi Goswami

Reputation: 23

Syntax Error : with tf.Session() as sess: File "<ipython-input-20-b0b30aac112f>",

I tried to run the following code, but got the syntax error

import tensorflow as tf
p = tf.constant("Hello")
h = tf.constant("world!")
ph = p + h
with tf.Session() as sess:
 sess.run(ph)
print(sess.run(ph))

The error is like the one in image below: Syntax Error

Upvotes: 1

Views: 1111

Answers (1)

vineet gundecha
vineet gundecha

Reputation: 166

You should include the print statement inside the with block

import tensorflow as tf
p = tf.constant("Hello")
h = tf.constant("world!")
ph = p + h
with tf.Session() as sess:
 print(sess.run(ph))

As for the error, you were probably trying to run the code interactively in ipython and might have hit shift+Enter while running the with statement. Ipython complained because it did not find anything inside the block

Upvotes: 1

Related Questions