【问题标题】:ImportError: Unable to import required dependencies: numpyImportError:无法导入所需的依赖项:numpy
【发布时间】:2022-01-05 23:48:16
【问题描述】:

运行以下python代码 使用命令 python3 在 vscode 和终端中完全可以正常工作

import sys as sys
import pandas as ps
import os
print("PYTHONPATH:", os.environ.get('PYTHONPATH'))
print("PATH:", os.environ.get('PATH'))
print('testing...')
sys.stdout.flush()

但是当我尝试使用节点 child_process 运行它时 使用以下代码:

const {spawn,exec} = require('child_process')



const pyProcess = spawn('python3',['./python/test.py']);

var result = ''
pyProcess.stdout.on('data',(data) => result += data.toString())


pyProcess.stdout.on('end',() => console.log(result))
pyProcess.stderr.on('data', (data) => {
    console.error(`stderr: ${data}`);
  });


它返回这个包导入错误:

stderr:     import pandas as ps
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/__init__.py", line 16, in <module>
    raise ImportError(
ImportError: Unable to import required dependencies:
numpy: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.10 from "/Library/Frameworks/Python.framework/Versions/3.10/bin/python3"
  * The NumPy version is: "1.22.0"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: dlopen(/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-darwin.so, 0x0002): tried: '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-darwin.so' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/usr/lib/_multiarray_umath.cpython-310-darwin.so' (no such file)

**注意:此错误仅在 Mac OS m1 芯片设备上运行时出现, 适用于 Windows pc **

【问题讨论】:

  • 您仔细研究了上面链接的文档吗?
  • 是的,没有帮助。

标签: python node.js python-3.x numpy child-process


【解决方案1】:

解释:

终端

通过终端执行 python 代码运行良好,因为 它为 arm64 架构编译,这是预期的

Node.js 子进程

但是...当运行 Node.js Child_process 时,shell 正在 EMACS 内执行,它为 x86_64 架构编译,而不是预期并返回错误

参考:Setting up the Apple M1 for Native Code Development...

解决方案

您需要指定编译器以最终针对预期的架构进行编译。

通过编辑执行到arch -arch arm64 &lt;command&gt;的命令 您指定使用 arm64 编译器,它就可以完成这项工作。

【讨论】:

    猜你喜欢
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 2016-12-27
    相关资源
    最近更新 更多