Tweepy: ImportError: cannot import name Random

走远了吗. 提交于 2020-01-10 02:34:06

问题


I'm using Tweepy to send some messages to Twitter. I'm getting a long traceback from the API. The traceback settles at:

ImportError: cannot import name Random

I used Pip to install the latest version of Tweepy:

Name: tweepy
Version: 2.3.0
Location: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Requires:

When I call

import tweepy

I get this traceback:

Traceback (most recent call last):
  File "/Users/dromard/Projects/Drop Playlist/drop.py", line 4, in <module>
    import tweepy
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tweepy/__init__.py", line 14, in <module>
    from tweepy.api import API
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tweepy/api.py", line 8, in <module>
    from tweepy.binder import bind_api
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tweepy/binder.py", line 5, in <module>
    import httplib
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 79, in <module>
    import mimetools
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/mimetools.py", line 6, in <module>
    import tempfile
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 35, in <module>
    from random import Random as _Random
ImportError: cannot import name Random

Process finished with exit code 1

I'm working in PyCharm and confirmed the site package and Python paths are correct in settings. I manually checked in console that the paths are correct, and there are no duplicate locations.

I haven't made any changes to Tweepy. I let Pip install it where it is, as it is. Permissions look correct:

-rw-r--r--   1 root  wheel

If I check Python in console:
- I get the same traceback
- When I run the individual imports, they all execute without error

It all fails out at the random call. I think random is part of Python's core packages, and not part of Tweepy.

I handed this script off to a co-worker, who then used Pip to install tweepy and hit the same traceback. Makes me think Pip might be contributing.

I'm relatively new to Python (programming in general). I looked through other 'import error' articles, but didn't find this specific issue. Any help is appreciated.


回答1:


I figured this out. I had created a python file called 'random.py' during the course of experimenting with a random number generating script. My 'import random' call was grabbing this file, which lacked the library Random. It essentially created a conflict with the proper 'random.'




回答2:


Make sure that you delete the random.py file or overwrite it with another name, then recheck "from random import random".



来源:https://stackoverflow.com/questions/25750961/tweepy-importerror-cannot-import-name-random

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!