Error 'tf' not found

I have installed tensorflow in the night build. And I am importing tensorflow as tf in my code. But I get an error stating “tf” not found. This is solved by importing tensorflow as tf in the python interpreter. Can you please tell me what I am doing wrong?


Did you install TensorFlow with this command in a recent version of Slicer?
Then, you should be able to import and use TensorFlow.
We experienced an error recently on Windows, and the solution was to install Visual Studio on the same computer. Apparently, the Windows version of TensorFlow requires something from Visual Studio that it doesn’t package automatically.

I had installed tensorflow using slicer.util.pip_install(“tensorflow”). And i am using a Linux machine.
I tried using the pip_install command but the same issue is there where I still have to manually import tensorflow in the python interpreter rather than it automatically being done in *.py file

If your .py file is a module for 3D Slicer, and you have installed TensorFlow with the slicer.util.pip_install() command, then you can just add import tensorflow at the beginning of your .py file, and that should work.

Traceback (most recent call last):
File “/home/tpk11/Slicer_MimickNet/MimickNet/”, line 118, in onApplyButton,self.modelPathEdit.currentPath)
File “/home/tpk11/Slicer_MimickNet/MimickNet/”, line 168, in run
model = tf.keras.models.load_model(path)
File “/scratch/tpk11/Slicer/lib/Python/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/”, line 146, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
File “/scratch/tpk11/Slicer/lib/Python/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/”, line 168, in load_model_from_hdf5
File “/scratch/tpk11/Slicer/lib/Python/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/”, line 55, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File “/scratch/tpk11/Slicer/lib/Python/lib/python3.6/site-packages/tensorflow_core/python/keras/layers/”, line 106, in deserialize
File “/scratch/tpk11/Slicer/lib/Python/lib/python3.6/site-packages/tensorflow_core/python/keras/utils/”, line 303, in deserialize_keras_object
File “/scratch/tpk11/Slicer/lib/Python/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/”, line 937, in from_config
config, custom_objects)
File “/scratch/tpk11/Slicer/lib/Python/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/”, line 1903, in reconstruct_from_config
process_node(layer, node_data)
File “/scratch/tpk11/Slicer/lib/Python/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/”, line 1851, in process_node
output_tensors = layer(input_tensors, **kwargs)
File “/scratch/tpk11/Slicer/lib/Python/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/”, line 773, in call
outputs = call_fn(cast_inputs, *args, **kwargs)
File “/scratch/tpk11/Slicer/lib/Python/lib/python3.6/site-packages/tensorflow_core/python/keras/layers/”, line 846, in call
result = self.function(inputs, **kwargs)
File “”, line 2, in custom_pad
NameError: name ‘tf’ is not defined

This is the error i get. Tensorflow is being imported in the module .py file