[python] TensorFlow, "'module' object has no attribute 'placeholder'"

I've been trying to use tensorflow for two days now installing and reinstalling it over and over again in python2.7 and 3.4. No matter what I do, I get this error message when trying to use tensorflow.placeholder()

It's very boilerplate code:

tf_in = tf.placeholder("float", [None, A]) # Features

No matter what I do I always get the trace back:

Traceback (most recent call last):
  File "/home/willim/PycharmProjects/tensorflow/tensorflow.py", line 2, in <module>
    import tensorflow as tf
  File "/home/willim/PycharmProjects/tensorflow/tensorflow.py", line 53, in <module>
    tf_in = tf.placeholder("float", [None, A]) # Features
AttributeError: 'module' object has no attribute 'placeholder'

Anyone know how I can fix this?

This question is related to python machine-learning tensorflow

The answer is


It appears that .placeholder() , .reset_default_graph() , and others were removed with version 2. I ran into this issue using Docker image: tensorflow/tensorflow:latest-gpu-py3 which automatically pulls the latest version. I was working in 1.13.1 and was 'upgraded to 2' automatically and started getting the error messages. I fixed this by being more specific with my image: tensorflow/tensorflow:1.13.1-gpu-py3.

More info can be found here: https://www.tensorflow.org/alpha/guide/effective_tf2


The problem is with TensorFlow version; the one you are running is 2.0 or something above 1.5, while placeholder can only work with 1.4.

So simply uninstall TensorFlow, then install it again with version 1.4 and everything will work.


I had the same problem before after tried to upgrade tensorflow, I solved it by reinstalling Tensorflow and Keras.

pip uninstall tensorflow

pip uninstall keras

Then:

pip install tensorflow

pip install keras


It may be the typo if you incorrectly wrote the placeholder word. In my case I misspelled it as placehoder and got the error like this: AttributeError: 'module' object has no attribute 'placehoder'


Avoid using the below striked out statement in tensorflow=2.0

i?m?p?o?r?t? ?t?e?n?s?o?r?f?l?o?w? ?a?s? ?t?f? ?x? ?=? ?t?f?.?p?l?a?c?e?h?o?l?d?e?r?(?s?h?a?p?e?=?[?N?o?n?e?,? ?2?]?,? ?d?t?y?p?e?=?t?f?.?f?l?o?a?t?3?2?)?

You can disable the v2 behavior by using the following code

This one is perfectly working for me.

import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()
x = tf.placeholder(shape=[None, 2], dtype=tf.float32)


If you have this error after an upgrade to TensorFlow 2.0, you can still use 1.X API by replacing:

import tensorflow as tf

by

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

You need to use the keras model with tensorflow 2, as here

import tensorflow as tf
from tensorflow.python.keras.layers import  Input, Embedding, Dot, Reshape, Dense
from tensorflow.python.keras.models import Model

The error shows up because we are using tensorflow version 2 and the command is from version 1. So if we use:

tf.compat.v1.summary.(method_name)

It'll work


Recent version 2.0 does not support placeholder. I uninstalled 2.0 using command: conda remove tensorflow. then I installed 1.15.0 using command: conda install -c conda-forge tensorflow=1.15.0. 1.15 is latest in version 1 series. You can change as per you wish and requirement. For seeing all version, use command: conda search tensorflow. It worked for Anaconda3 in Windows.


If you are using TensorFlow 2.0, then some code developed for tf 1.x may not code work. Either you can follow the link : https://www.tensorflow.org/guide/migrate

or you can install a previous version of tf by pip3 install tensorflow==version


If you get this on tensorflow 2.0.0+, it's very likely because the code isn't compatible with the newer version of tensorflow.

To fix this, run the tf_upgrade_v2 script.

tf_upgrade_v2 --infile=YOUR_SCRIPT.py --outfile=YOUR_SCRIPT.py

Import the old version of tensorflow instead of the new version

[https://inneka.com/ml/tf/tensorflow-module-object-has-no-attribute-placeholder/][1]

import tensorflow.compat.v1 as tf tf.disable_v2_behavior()


I also got the same error. May be because of the version of tensorflow. After installing tensorflow 1.4.0, I got relief from the error.

pip install tensorflow==1.4.0

Try this:

pip install tensorflow==1.14

or this (if you have GPU):

pip install tensorflow-gpu==1.14

import tensorflow.compat.v1 as tf

tf.disable_v2_behavior() 

works. I am using Python 3.7 and tensorflow 2.0.


Faced same issue on Ubuntu 16LTS when tensor flow was installed over existing python installation.

Workaround: 1.)Uninstall tensorflow from pip and pip3

sudo pip uninstall tensorflow
sudo pip3 uninstall tensorflow

2.)Uninstall python & python3

sudo apt-get remove python-dev python3-dev python-pip python3-pip

3.)Install only a single version of python(I used python 3)

sudo apt-get install python3-dev python3-pip

4.)Install tensorflow to python3

sudo pip3 install --upgrade pip

for non GPU tensorflow, run this command

sudo pip3 install --upgrade tensorflow

for GPU tensorflow, run below command

sudo pip3 install --upgrade tensorflow-gpu

Suggest not to install GPU and vanilla version of tensorflow


Because you cant use placeholder in tensflow2.0version, so you need to use tensflow1*, or you need to change your code to fix tensflow2.0


Solution: Do not use "tensorflow" as your filename.

Notice that you use tensorflow.py as your filename. And I guess you write code like:

import tensorflow as tf

Then you are actually importing the script file "tensorflow.py" that is under your current working directory, rather than the "real" tensorflow module from Google.

Here is the order in which a module will be searched when importing:

  1. The directory containing the input script (or the current directory when no file is specified).

  2. PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

  3. The installation-dependent default.


Instead of tf.placeholder(shape=[None, 2], dtype=tf.float32) use something like tf.compat.v1.placeholder(shape=[None, 2], dtype=tf.float32) if you don't want to disable v2 completely.


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to machine-learning

Error in Python script "Expected 2D array, got 1D array instead:"? How to predict input image using trained model in Keras? What is the role of "Flatten" in Keras? How to concatenate two layers in keras? How to save final model using keras? scikit-learn random state in splitting dataset Why binary_crossentropy and categorical_crossentropy give different performances for the same problem? What is the meaning of the word logits in TensorFlow? Can anyone explain me StandardScaler? Can Keras with Tensorflow backend be forced to use CPU or GPU at will?

Examples related to tensorflow

Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation Module 'tensorflow' has no attribute 'contrib' Tensorflow 2.0 - AttributeError: module 'tensorflow' has no attribute 'Session' Could not install packages due to an EnvironmentError: [WinError 5] Access is denied: How do I use TensorFlow GPU? Which TensorFlow and CUDA version combinations are compatible? Could not find a version that satisfies the requirement tensorflow pip3: command not found How to import keras from tf.keras in Tensorflow? Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2