Niek de Klein
Niek de Klein

Reputation: 8834

Importing numpy gives ImportError: cannot import name TestCase

I'm working on a remote desktop server, Windows XP Profesional. About 2 hours ago numpy was still working on there. I was trying to paralelise a for loop using mulitprocessing.Pool() and I must have done something wrong, because after a short time there were 100ish python.exe processes running. It took me some time to get back in on the server, and I wanted to test why all those processes got made. However, I ran into the following error:

>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\numpy\__init__.py", line 154, in <module>
    import add_newdocs
  File "C:\Python27\lib\site-packages\numpy\add_newdocs.py", line 9, in <module>

    from numpy.lib import add_newdoc
  File "C:\Python27\lib\site-packages\numpy\lib\__init__.py", line 4, in <module
>
    from type_check import *
  File "C:\Python27\lib\site-packages\numpy\lib\type_check.py", line 8, in <modu
le>
    import numpy.core.numeric as _nx
  File "C:\Python27\lib\site-packages\numpy\core\__init__.py", line 45, in <modu
le>
    from numpy.testing import Tester
  File "C:\Python27\lib\site-packages\numpy\testing\__init__.py", line 8, in <mo
dule>
    from unittest import TestCase
ImportError: cannot import name TestCase

I can import unittest.py, but I can't import TestCase:

>>> import unittest
>>> from unittest import TestCase
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name TestCase

I have the same problem running python from Eclipse as running it from the command line. However, when using different Python (also 2.7, but 32-bits) it does work.

I first tried reinstalling numpy, but this did not solve the problem. The only other solution I found is the answer to this question. However, I've looked and deleted all unittest.py files, and I still have the same problem. I don't know what to try next.

Upvotes: 3

Views: 9898

Answers (1)

ev-br
ev-br

Reputation: 26090

(moving it from comments)

Apparently, .pyc files can get stale. Quoting http://nedbatchelder.com/blog/201310/finding_stale_pyc_files.html:

Recently I was debugging one of those "it can't happen" kinds of problems, and wanted to make sure I didn't have any stale .pyc files lying around.

So, the first thing to try is to check what exactly is being imported: print(unittest.__file__). You've checked that it's not a stray unittest.py in the working directory. OK. If it's a .pyc file, just try deleting it. No harm possible, it'll get regenerated next time you're importing it.

I've to admit I've no intelligent answer to the 'why' question. If anybody more knowledgeable than I can answer it, I'd happily upvote and delete this answer.

Upvotes: 7

Related Questions