0

My file is named "foo.py". It has only two lines.

import random
print(random.randint(10))

The error is...

    Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py", line 45, in <module>
    from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
  File "math.py", line 2, in <module>
    from random import randint
ImportError: cannot import name randint
  1. I am on MacOS 10.14.6
  2. I note that I did not call random.randint() in this script, even though it showed up in the error text.
  3. I ran the script with $python
  4. My /usr/bin/python is linked to python 2.7
  5. I've tried this with python 3 as well, with the same error.

EDIT:

My script was originally named "math.py", but I changed it in response to another solution that pointed out the name conflict with the math.py library (even though my script was not importing that library). Even after my script name change, I'm still seeing --File "math.py"-- errors. Even after I'm no longer using random.randint(), I'm still seeing that function referenced in my errors.

I've tried deleting random.pyc and math.pyc to purge the artifacts of previous executions. But these do not see to eliminate the remnants of earlier errors.

  • 2
    It appears that you have file named `math.py` which the random module is trying to import over the builtin math module? If this is the case, rename this module something else. – TheLazyScripter Oct 13 '19 at 23:33
  • Just want to make sure you're aware that support for 2.x officially ends at the end of the year. – Karl Knechtel Oct 14 '19 at 00:24

1 Answers1

0

Read the traceback:

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py"

Python tries to do something inside the standard library random module...

from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil

in particular, it tries to import the standard library math module...

File "math.py", line 2, in <module>

but it gets yours instead (notice there's no path on the filename this time; it's just math.py in the current directory); i.e. the script you started from. Python detects the circular import and fails:

ImportError: cannot import name randint

Actually using randint doesn't matter, because this is a problem with the actual import of the module.

This happens because Python is configured by default (using sys.path, which is a list of paths to try, in order) to try to import scripts from the current working directory before looking anywhere else. It's convenient when you just want to write a few source files in the same folder and have them work with each other, but it causes these problems.

The expected solution is to just rename your file. Unfortunately there isn't an obvious list of names to avoid, although you could peek at your installation folder to be sure (or just check the online library reference, though that's not quite so direct).

I guess you could also modify sys.path:

import sys
sys.path.remove('') # the empty string in this list is for the current directory
sys.path.append('') # put it back, at the end this time
import random # now Python will look for modules in the standard library first,
# and only in the current folder as a last resort.

However, this is an ugly hack. It might break something else (and it can't save you if you have a local sys.py).

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153