简体   繁体   中英

ImportError: Unable to import required dependencies: numpy

Running the following python code works totally fine in vscode and terminal using command python3

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()

but when I try to run it using node child_process with the following code:

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}`);
  });


it returns this package importing error:

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)

**Note: this error only showed up when running on Mac OS m1 chip device, worked fine for Windows pc **

Explanation:

terminal

Executing the python code through the terminal worked fine because its compiling for the arm64 architecture which is the expected

Node.js child Process

but... when running the Node.js Child_process the shell is executing inside EMACS which compiles for x86_64 architecture instead, and its not expected and returns the error

ref: Setting up the Apple M1 for Native Code Development...

Solution

You need to specify the Compiler to end up compiling for the expected architecture.

By editing the command executed to arch -arch arm64 <command> you specify using the arm64 compiler and it does the job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM