18

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.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Damian Romard
  • 663
  • 1
  • 5
  • 10
  • What happens if you run `from random import Random as _Random` from the Python prompt in Terminal? – MattDMo Sep 09 '14 at 18:33
  • @MattDMo I get this `>>> import random 9 >>> from random import Random as _Random Traceback (most recent call last): File "", line 1, in ImportError: cannot import name Random >>>` – Damian Romard Sep 09 '14 at 20:05
  • Possible duplicate of [from Crypto import Random -> ImportError: cannot import name Random](https://stackoverflow.com/questions/7210873/from-crypto-import-random-importerror-cannot-import-name-random). See @phihag's answer and discussion about a duplicate filename. – jww Jun 08 '17 at 02:56

3 Answers3

48

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.'

Damian Romard
  • 663
  • 1
  • 5
  • 10
4

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

joe_young
  • 4,107
  • 2
  • 27
  • 38
1

I could not comment due to low experience. I also had a xyz.py in a directory and calling a module of same name (import xyz) causes this error.

Make sure you do not save any file xyz.py that also have a python module at the same name. This is what I found from my newbie experiment.

This also happens with python 3.8.