hileez / node-pyrunner

Nodejs call python native addon.
MIT License
36 stars 2 forks source link

如何引入第三方python包 #3

Closed DawnLou closed 1 year ago

DawnLou commented 1 year ago

请问如何引入第三方的python包 比如numpy

DawnLou commented 1 year ago

是需要把site-packages拷贝过来么?

hileez commented 1 year ago

使用第三方pip模块,可以在当前NPM项目中创建pyscript目录作为普通python项目进行管理, 在该目录下使用python -m venv venv命令创建venv虚拟环境,在激活情况下pip的模块将会安装在该venv环境中, node-pyrunner将/pyscript/venv/Lib/site-packages路径作为默认的虚拟环境模块路径搜索并使用模块,

另外,也可以通过config配置模块搜索路径,例如:

const pyrunner = require('node-pyrunner')
// 模块搜索路径配置项,添加搜索路径
pyrunner.config['module_search_paths'].push('./pyscript');
// 虚拟环境配置项,具体到site-packages目录
pyrunner.config['pythonpath_env'] = './pyscript/venv/Lib/site-packages';
// 解释器安装目录,默认为当前项目内置解释器即/python/win32/x64/3.10.10目录,因此解释器内置标准模块都是可以使用的
pyrunner.config['python_home'] = 'Python解释器所在目录'

建议使用绝对路径配置,避免electron打包后的相对路径问题。

你也可以参考这个项目,快速创建应用: https://github.com/supercoderlee/node-pyrunner-quick-start

905968561 commented 1 year ago

pandas numpy sklearn这些包导入后会报 raise ImportError( ImportError: Unable to import required dependencies: numpy: Error importing numpy: you should not try to import numpy from its source directory; please exit the numpy source tree, and relaunch your python interpreter from there.