导入 numpy 时出错 [英] error importing numpy

查看:142
本文介绍了导入 numpy 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试导入 numpy 时出现奇怪的错误:

回溯(最近一次调用最后一次):文件/home/timo/malltul/mafet/src/mafet/core/pattern.py",第7行,在<module>将 numpy 导入为 np文件/usr/lib/python2.6/dist-packages/numpy/__init__.py",第147行,在<module>进口马文件/usr/lib/python2.6/dist-packages/numpy/ma/__init__.py",第44行,在<module>进口核心文件/usr/lib/python2.6/dist-packages/numpy/ma/core.py",第4850行,在<module>all = _frommethod('all')文件/usr/lib/python2.6/dist-packages/numpy/ma/core.py",第 4824 行,在 __init__ 中self.__doc__ = self.getdoc()文件/usr/lib/python2.6/dist-packages/numpy/ma/core.py",第 4830 行,在 getdoc 中签名 = self.__name__ + get_object_signature(meth)文件/usr/lib/python2.6/dist-packages/numpy/ma/core.py",第 109 行,在 get_object_signature进口检验 中的文件/usr/lib/python2.6/inspect.py",第 39 行导入标记化文件/usr/lib/python2.6/tokenize.py",第 38 行,在 <module> 中评论 = N_TOKENSNameError: name 'N_TOKENS' 未定义

问题的原因似乎是我的脚本在我自己的名为 core 的包中,每当我尝试在那里导入 numpy 时,我都会收到错误消息.在其他地方导入工作正常.

到目前为止,我唯一的解决方案是将我的核心"包重命名为其他名称.为什么这很重要?我做错了什么吗?

我在 Ubuntu 10.14 上使用 Python2.6.Numpy 版本是 1.3.0.

实际上重命名我的包并不能修复它.在我的包中重命名 token.py 修复了它.抱歉出现错误.

解决方案

我怀疑这与您的 core 模块或 numpy 有什么关系.

从堆栈跟踪来看,问题似乎出在 tokenize 模块上,该模块是 Python 的一部分,而不是 numpy 的一部分.Tokenize 执行 from token import * ,然后使用 token.py 中定义的 N_TOKENS.

首先,我会检查您的 PYTHONPATH 上是否没有名为 token 的杂散模块:

<预><代码>>>>导入令牌>>>令牌.__文件__'/usr/lib/python2.6/token.pyc'

如果选择了上述文件但问题仍然存在,我建议您重新安装 Python.

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.

解决方案

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.

这篇关于导入 numpy 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆