Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tweepy: ImportError: cannot import name Random

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.

like image 738
Damian Romard Avatar asked Sep 09 '14 17:09

Damian Romard


People also ask

How to resolve the import error cannot import name in Python?

To resolve the ImportError: Cannot import name, modify the x.py file. Instead of importing the y module at the start of the x.py file, write at the end of the file. Now rerun, and you can see the following output.

What is the name of the Python file where random is imported?

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.

Is random a part of tweepy?

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

Why am I getting “import XYZ” error in Python?

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.


1 Answers

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

like image 111
Damian Romard Avatar answered Sep 18 '22 09:09

Damian Romard