norman
norman

Reputation: 5556

Python Facebook SDK: 'module' object has no attribute 'GraphAPI'

I am trying to run a basic example with the Facebook SDK for Python. I tried doing exactly what this tutorial does from the command line (with "pip install facebook" and "pip install facebook-sdk" successfully done first...):

enter image description here

import Facebook works okay, but graph = facebook.GraphAPI() gives the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'GraphAPI'

I'm not imtimately familiar with how modules work, but it looks like facebook somehow isn't recognized at all! Or something else is missing here. From the command line, I tried these things to investigate:

>>>dir(facebook)

Output:

['__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__version__']

and...

>>>help(facebook)

Output:

Help on package facebook:

NAME
    facebook - TODO: Document your package.

FILE
    /Library/Python/2.7/site-packages/Facebook-0.0-py2.7.egg/facebook/__init__.py

PACKAGE CONTENTS


DATA
    __loader__ = <zipimporter object "/Library/Python/2.7/site-packages/Fa...
    __version__ = 'TODO: Enter a version'

VERSION
    TODO: Enter a version

(END) 

...But I'm still stuck. Do I need to "enter a version" to somehow get the thing to fully instantiate? Or could it be something else?

Upvotes: 20

Views: 16292

Answers (5)

Kartik Prajapati
Kartik Prajapati

Reputation: 21

I had the same problem. I renamed the file from facebook.py to first.py and it worked! So, my conclusion is don't save file with a module name.

Upvotes: 1

Felipe Valera
Felipe Valera

Reputation: 53

I checked everything because i had the same problem, i uninstalled and installed as the the recommendations, i checked if there is another file, package facebook, and no worked, then i saw i had this in my program graph = facebook.GraphApi(token) , ....Api.. A uppercase and the following letters lowercase, then I typed like this, Result graph = facebook.GraphAPI(token) , API uppercase all of them, now after i validated access token and checked other little bug everything was working PERFECTLY.

Good luck.

Upvotes: 0

Meistro
Meistro

Reputation: 3794

I tried what @thefourtheye suggested but pip wasn't uninstalling everything. Instead, I just cleaned out the Facebook packages via

rm -rf /path/to/my/venv/lib/python2.7/site-packages/facebook* && rm -rf /path/to/my/venv/lib/python2.7/site-packages/Facebook* 

WARNING: check that you don't have any other packages that start with facebook before you go removing anything. In my case I had

facebook/    
Facebook-0.0-py2.7.egg-info/   
facebook.py    
facebook.pyc 
facebook_sdk-0.4.0-py2.7.egg-info/ 

Upvotes: 1

user3878801
user3878801

Reputation: 461

I had the same problem when messing around with facebook-sdk for python the first time. It occured I named my python file "facebook.py", and made unconsiously a name clash.

Upvotes: 36

thefourtheye
thefourtheye

Reputation: 239443

If you are using Ubuntu or Debian, Just execute the following commands to get this working

sudo pip uninstall facebook
sudo pip uninstall facebook-sdk
sudo pip install facebook-sdk

For other operating systems, just remove facebook and facebook-sdk packages and install only facebook-sdk.

And then execute that program, it will work. Looks like facebook module is a dummy module. What we actually need is facebook-sdk only.

Upvotes: 22

Related Questions