2

i'm trying to execute this python script :

#! /usr/bin/python

import stackexchange
so = stackexchange.Site(stackexchange.StackOverflow)

my_favourite_guy = so.user(41981)
print my_favourite_guy.reputation.format()
print len(my_favourite_guy.answers), 'answers'

But i got this error

Jarvis:Documents Adeel$ python stackexchange.py
Traceback (most recent call last):
  File "stackexchange.py", line 3, in <module>
    import stackexchange
  File "/Users/Adeel/Documents/stackexchange.py", line 5, in <module>
    so = stackexchange.Site(stackexchange.StackOverflow)
AttributeError: 'module' object has no attribute 'Site'

It works fine when I copy and paste it on the python shell :

Jarvis:~ Adeel$ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import stackexchange
>>> so = stackexchange.Site(stackexchange.StackOverflow)
>>> my_favourite_guy = so.user(41981)
>>> print my_favourite_guy.reputation.format()
11.2k
>>> print len(my_favourite_guy.answers), 'answers'
239 answers

I don't understand why..can anyone help me ?

1 Answer 1

1

Your file is named stackexchange.py, so Python is recursively loading it instead. This is a very common Python pitfall:

Fix

Change the filename: call your script se-test.py or something.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .