error importing numpy

2019-07-22 06:29发布

问题:

I have strange error, when I try to import numpy:

Traceback (most recent call last):
  File "/home/timo/malltul/mafet/src/mafet/core/pattern.py", line 7, in <module>
    import numpy as np
  File "/usr/lib/python2.6/dist-packages/numpy/__init__.py", line 147, in <module>
    import ma
  File "/usr/lib/python2.6/dist-packages/numpy/ma/__init__.py", line 44, in <module>
    import core
  File "/usr/lib/python2.6/dist-packages/numpy/ma/core.py", line 4850, in <module>
    all = _frommethod('all')
  File "/usr/lib/python2.6/dist-packages/numpy/ma/core.py", line 4824, in __init__
    self.__doc__ = self.getdoc()
  File "/usr/lib/python2.6/dist-packages/numpy/ma/core.py", line 4830, in getdoc
    signature = self.__name__ + get_object_signature(meth)
  File "/usr/lib/python2.6/dist-packages/numpy/ma/core.py", line 109, in get_object_signature
    import inspect
  File "/usr/lib/python2.6/inspect.py", line 39, in <module>
    import tokenize
  File "/usr/lib/python2.6/tokenize.py", line 38, in <module>
    COMMENT = N_TOKENS
NameError: name 'N_TOKENS' is not defined

It seems that the cause of the problem is that my script is in my own package named core and whenever I try to import numpy there, I get the error. Importing works fine elsewhere.

The only solution I've got this far is to rename my 'core' package to something else. Why does this matter? Am I doing something wrong?

I'm using Python2.6 on Ubuntu 10.14 . Numpy version is 1.3.0 .

EDIT: Actually renaming my package does not fix it. Renaming token.py in my package fixes it. Sorry for the error.

回答1:

I doubt this has anything to do with your core module or with numpy.

From the stack trace, it would appear that the problem is with the tokenize module, which is part of Python, not part of numpy. Tokenize does from token import * and then uses N_TOKENS that's defined in token.py.

First of all, I'd check that there's no stray module called token on your PYTHONPATH:

>>> import token
>>> token.__file__
'/usr/lib/python2.6/token.pyc'

If this picks up the above file yet you still get the problem, I'd suggest reinstalling Python.