使用“knee.py”导入 numpy 和 scipy 时出现神秘错误

发布于 2024-11-24 10:30:10 字数 1126 浏览 2 评论 0原文

Python 源代码中包含一个名为 knee.py 的文件。它用自己的钩子覆盖常规导入语句。代码应该是正确的,对于内置模块和单文件模块来说,确实如此。然而,当我尝试用它导入 numpy 时,我收到一个奇怪的错误(两者都不同)。

(...long stack trace)
  File "knee.py", line 101, in import_module
    m = imp.load_module(fqname, fp, pathname, stuff)
TypeError: import_hook() takes at most 4 arguments (5 given)

我不明白为什么会发生这种事?是否还传递了一个 selfknee.py 中的代码都不是为对象编写的,而且我很确定 imp.load_module 不需要 self 论证。有谁知道可能导致问题的原因是什么?我什至记录了导入的内容,似乎代码并没有立即遇到问题,只有在导入了 numpy 中的其他内容之后才遇到问题。

对于 scipy 的情况,我收到一个错误,这可能与 numpy 事件有关:

(...long stack trace)
  File "/usr/apps/python2.6/lib/python2.6/site-packages/numpy/core/__init__.py", line 10, in <module>
    import _sort
  File "knee.py", line 16, in import_hook
    q, tail = find_head_package(parent, name)
  File "knee.py", line 52, in find_head_package
    q = import_module(head, qname, parent)
  File "knee.py", line 101, in import_module
    m = imp.load_module(fqname, fp, pathname, stuff)
ImportError: numpy.core.multiarray failed to import

有人知道会发生什么吗?

There's a file included with the Python source called knee.py. It overrides the regular import statement with it's own hook. The code is supposed to be correct, and for builtin modules and single file modules, it is. However, I get a bizarre error (both different) when I try to import numpy with it.

(...long stack trace)
  File "knee.py", line 101, in import_module
    m = imp.load_module(fqname, fp, pathname, stuff)
TypeError: import_hook() takes at most 4 arguments (5 given)

I don't get why this would happen? Could it be that there's a self being passed as well? None of the code in knee.py is written for an object, and I'm pretty sure that imp.load_module doesn't expect a self argument. Does anyone have an idea as to what could be causing the problem? I even kept a record of what was getting imported, and it seems that the code doesn't run into the problem right away, only after it's imported some other things in numpy.

For the case with scipy, I get an error, which may be related to the numpy incident:

(...long stack trace)
  File "/usr/apps/python2.6/lib/python2.6/site-packages/numpy/core/__init__.py", line 10, in <module>
    import _sort
  File "knee.py", line 16, in import_hook
    q, tail = find_head_package(parent, name)
  File "knee.py", line 52, in find_head_package
    q = import_module(head, qname, parent)
  File "knee.py", line 101, in import_module
    m = imp.load_module(fqname, fp, pathname, stuff)
ImportError: numpy.core.multiarray failed to import

Does anyone have an idea of what could be going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光无声 2024-12-01 10:30:10

已解决。 (仅保证打包模块 numpy 和 scipy 的解决方案)

我实现了自己的解决方案。请注意,虽然我在这里发布的代码可以工作,但它包含一堆愚蠢的调试语句和注释掉的可以删除的代码,以及做一些与我想要使用它的目的相关的其他事情。

我的解决方案: mknee.py

SOLVED. (only guaranteeing solution for packaged modules numpy and scipy)

I implemented my own solution. Be wary that while the code I post here works, it contains a bunch of silly debug statements and commented out code that can be removed, as well as doing a couple of other things that are related to what I wanted to use this for.

My solution: mknee.py

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文