Question

For the wikipedia module of python (installed through pip)

When I use ipython and do the following:

import wikipedia
print wikipedia.summary("Wikipedia")

..it works fine. But when I save the same in a file and run it from shell through:

python filename.py

It shows AttributeError: 'module' object has no attribute 'summary'

Why this behavior?

PS: I later included the following in 'filename.py', to list this module's scope:

print dir(wikipedia) 

.. this gives:

['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'wikipedia']

while in ipython, dir(wikipedia) gives all names as expected:

['API_URL', ..(snip) .. 'PageError', 'RATE_LIMIT', .. (snip) ..  '__name__', 'time', 'timedelta']

Why so?

Was it helpful?

Solution

10 to 1 you named your own script wikipedia.py.

The line import wikipedia then imports the script itself, not the package you installed from PyPI. You can see this in the dir() output, the imported module has the name wikipedia in the namespace; the self-reference.

You can confirm this, or find what other module masked your import, with:

import wikipedia
print wikipedia.__file__

Rename that file to stop it from masking the library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top