脾气暴躁:RuntimeWarning:遇到溢出

时间:2018-11-11 01:35:12

标签: python numpy

我的一个朋友给我发送了一些代码,包括numpy,functools和matplotlib软件包,这些代码在他的Windows系统上可以正常工作,并且不会引发任何警告/异常。相同的代码在我的系统上不起作用,我得到很多朋友没有得到的“ -inf”或“ nan”值。

当我在MacBook(OS X 10.14),python 3.6和相同的已安装软件包上运行相同的代码时,出现以下错误:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/linalg/linalg.py:2022: RuntimeWarning: overflow encountered in det
  r = _umath_linalg.det(a, signature=signature)
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ipykernel_launcher.py:90: RuntimeWarning: invalid value encountered in multiply
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ipykernel_launcher.py:94: RuntimeWarning: invalid value encountered in multiply
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/linalg/linalg.py:2022: RuntimeWarning: invalid value encountered in det
  r = _umath_linalg.det(a, signature=signature)
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ipykernel_launcher.py:99: RuntimeWarning: invalid value encountered in multiply
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ipykernel_launcher.py:76: RuntimeWarning: divide by zero encountered in log`

我还注意到,当我尝试“ pip install functools”时,出现了一些错误消息,这些消息对我来说并不明确。新的Mojave更新是否可能破坏了我系统上的某些内容?

/usr/bin/clang -fno-strict-aliasing -Wsign-compare -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -arch i386 -arch x86_64 -g -I/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m -c src/functools.c -o build/temp.macosx-10.6-intel-3.6/src/functools.o
    src/functools.c:43:6: warning: implicit declaration of function 'Py_InitModule3' is invalid in C99 [-Wimplicit-function-declaration]
            m = Py_InitModule3("_functools", NULL, functools_doc);
                ^
    src/functools.c:43:6: warning: this function declaration is not a prototype [-Wstrict-prototypes]
    src/functools.c:43:4: warning: incompatible integer to pointer conversion assigning to 'PyObject *' (aka 'struct _object *') from 'int' [-Wint-conversion]
            m = Py_InitModule3("_functools", NULL, functools_doc);
              ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    src/functools.c:45:3: error: non-void function 'init_functools' should return a value [-Wreturn-type]
                    return;
                    ^
    src/functools.c:49:2: error: non-void function 'init_functools' should return a value [-Wreturn-type]
            return;
            ^
    3 warnings and 2 errors generated.
    error: command '/usr/bin/clang' failed with exit status 1

Command "/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 -u -c "import setuptools, tokenize;__file__='/private/var/folders/hg/qhfjf8mn647dsz9p4c85j0z40000gn/T/pip-install-nek3bgo1/functools/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/var/folders/hg/qhfjf8mn647dsz9p4c85j0z40000gn/T/pip-record-6vakus33/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/hg/qhfjf8mn647dsz9p4c85j0z40000gn/T/pip-install-nek3bgo1/functools/`

编辑:为了更清楚一点,不仅是我收到了我的朋友没有得到的错误消息,而且代码在我的系统上也完全不起作用。

1 个答案:

答案 0 :(得分:0)

两件事:首先,functools是Python标准库的一部分。它是每个Python发行版的一部分,因此您不需要使用pip安装它。我相信您得到的错误归结于仍然存在functools package up on PyPIpip从中获取软件包的存储库)这一事实。但是,它的最新更新时间是2005年。它不是您想要的functools。不要运行pip install functools。您只需在任何Python代码中执行import functools,导入就可以正常工作。

第二,当您的朋友不在时,您会收到警告,这可能是由于很多不同的原因。您必须自己调试代码才能找出问题所在。最简单的方法是将警告更改为错误。在要执行的Python脚本顶部附近(在import语句之后)添加以下行:

np.seterr(all='raise')

当该行就位时,每当numpy发出警告时,它就会引发错误。该错误将包含一个堆栈跟踪,该跟踪会返回您自己代码中的错误行。这将帮助您开始找出问题所在。

相关问题