error importing numpy

删除回忆录丶 提交于 2019-12-24 01:26:07

问题


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.



来源:https://stackoverflow.com/questions/7414504/error-importing-numpy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!