Search code examples
pythonwindowsnumpyanacondaqutip

Python – ImportError: cannot import name X


I’m trying to run a single python script – which has worked in the past – using a couple modules. Some Googling suggested circular dependencies however that is only relevant for two files importing modules that depend on one another.

It seems to boil down to scalarmath not being found but I don't know why. My code is just simple functions defs for calculations and then calling those functions (all in one file).

The output of my IDE is:

C:\Users\sg15\AppData\Local\Continuum\Anaconda2\python.exe "P:/Solid State QT/NSMM/simulations/InteractionsOfTLF.py"
Traceback (most recent call last):
  File "P:/Solid State QT/NSMM/simulations/InteractionsOfTLF.py", line 13, in <module>
    import matplotlib.pyplot as plt
  File "C:\Users\sg15\AppData\Local\Continuum\Anaconda2\lib\site-packages\matplotlib\__init__.py", line 122, in <module>
    from matplotlib.cbook import is_string_like, mplDeprecation, dedent, get_label
  File "C:\Users\sg15\AppData\Local\Continuum\Anaconda2\lib\site-packages\matplotlib\cbook.py", line 33, in <module>
    import numpy as np
  File "C:\Users\sg15\AppData\Local\Continuum\Anaconda2\lib\site-packages\numpy\__init__.py", line 185, in <module>
    from . import add_newdocs
  File "C:\Users\sg15\AppData\Local\Continuum\Anaconda2\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "C:\Users\sg15\AppData\Local\Continuum\Anaconda2\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
    from .type_check import *
  File "C:\Users\sg15\AppData\Local\Continuum\Anaconda2\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "C:\Users\sg15\AppData\Local\Continuum\Anaconda2\lib\site-packages\numpy\core\__init__.py", line 20, in <module>
    from . import scalarmath
ImportError: cannot import name scalarmath

Process finished with exit code 1

Solution

  • It turns out the solution was a classic uninstall reinstall jobby. It seemed that the packages became incompatible with a recent Windows update or an update to the packages themselves. Steps to fix (in case you need them):

    1) Run python in a separate program, and try to import a library e.g. import numpy. You should still see the output error message in the image that was shown in the question. This indicates that it is not a local problem to our code. Close that python program/console/whatever.

    2) Open command prompt (cmd) and run conda (command: conda). Then run the commands conda install numpy and conda install matplotlib. Click yes [y] to any options that are presented. These commands depend on which libraries were causing you trouble, of course.

    3) In your python IDE's console, import the libraries again i.e. import numpy and import matplotlib. Run your code and you it should build just fine (providing you have no other bugs)

    Note: You may have to uninstall the offending libraries giving you trouble in your conda environment in the Anaconda Navigator. To do this, select your conda environment (mine was called qutip) and remove the troublesome packages. They were matplotlib and numpy in this case.