user3626345
user3626345

Reputation: 21

cannot import Python-Twitter

I recently started out with python and i'm right now looking to do an app that shows my Twitter feed. So I downloaded a module(not sure if its called that) called Python-Twitter and installed it. But now everytime I try to import it I just get this error:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import twitter
  File "build\bdist.win32\egg\twitter.py", line 38, in <module>
  File "C:\Python27\lib\site-packages\requests_oauthlib-0.4.0-py2.7.egg\requests_oauthlib\__init__.py", line 1, in <module>
    from .oauth1_auth import OAuth1
  File "C:\Python27\lib\site-packages\requests_oauthlib-0.4.0-py2.7.egg\requests_oauthlib\oauth1_auth.py", line 4, in <module>
    from requests.utils import to_native_string
ImportError: cannot import name to_native_string

Does anyone know what I might have done wrong with the install or something? Thx

Upvotes: 1

Views: 2366

Answers (3)

chhantyal
chhantyal

Reputation: 12262

This is because of requests version.

requests.utils.to_native_string is available since requests 2.0.0

So just update requests:

pip install -U requests

See more details here on another thread

Upvotes: 3

iain
iain

Reputation: 5683

In my case installing requests-oauthlib fixed it

sudo pip install requests-oauthlib

Upvotes: 6

khampson
khampson

Reputation: 15336

This sounds like an issue with your install. The method to_native_string should be defined in the requests\utils.py in your Python install.

I'm on Ubuntu, and installed the Twitter module, and can import it without any errors. When I look in my install, in /usr/lib/python2.7/dist-packages/requests/utils.py, there is a to_native_string method defined there.

The implication from the error you're getting is that in your installation, there is either no utils.py, or, if there is, it does not contain that method.

I would recommend checking your install to see if that is the case. If it is indeed missing, I would recommend axing your install and reinstalling. You might want to try a virtualenv environment instead, so it can act as a sandbox (in that case you can leave your current install as is, as long as it is intact enough to run virtualenv and pip).

If utils.py is actually there and does contain a method with that name, I would recommend running a debugger such as pudb or pdb (pudb isn't built in, but it's more full featured), and step through the import to see if that sheds any additional light.

Upvotes: 1

Related Questions