Electron, Nodejs, Zerorpc,Python 跨平台程序编译

Electron, Zerorpc,Python 跨平台程序编译

前言

Electron可以用来把Web程序包装成各种平台的客户端程序,编译和打包过程也是充满挑战。
一方面是因为所使用的库和软件,有些仍然在不断更新,有些却早已经停止更新了,从而导致版本依赖的不一致问题。

比较复杂的架构是,前端用nodejs实现,后端用python实现,中间通信可以用:zerorpc,ipc等。其中zerorpc的效率高,但最容易出问题,因为zerorpc使用了zeromq, zerorpc已经停止更新了,两者之间的依赖也早已停止更新。

环境

  1. Windows 10 / Ubuntu 16.04
    Windows 10
    node: 10.16.2
    npm: 6.9.0
    python: 3.6

当前conda的稳定版本,默认包含的python是3.7, 由于程序依赖问题,需要降级到3.6, 可以直接通过:


 conda install python=3.6
 

来实现。

  1. 更新安装源

默认的conda和npm的源,可能安装各个包,会比较慢,最好更新成国内源:

conda:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

npm:

npm config set registry http://registry.npm.taobao.org

可能遇到的各种问题

  1. NODE_MODULE_VERSION 版本不一致问题

    可能会报某个库的node版本与当前的node版本不一致

    扫描二维码关注公众号,回复: 11511216 查看本文章
    The module '/home/dev/Documents/project/node_web_kit/node_modules/node-pty/build/Release/pty.node' 
    was compiled against a different Node.js version using NODE_MODULE_VERSION 46. 
    This version of Node.js requires NODE_MODULE_VERSION 51. Please try re-compiling or re-installing
    
    

    遇到这种问题,有两种解决方式:

    1). 调整系统安装的node版本,让两者保持一致

    在这里插入图片描述
    具体的对应关系可以参照:nodejs release

2). 用Native Node Modules方式。

就是让相应的模块,可以保留自己的Node版本,通过rebuil的方式,来让不同的版本和谐共处。

npm install --save-dev electron-rebuild

# Every time you run "npm install", run this:
./node_modules/.bin/electron-rebuild

# On Windows if you have trouble, try:
.\node_modules\.bin\electron-rebuild.cmd

具体参照以下链接:using-native-node-modules

  1. zeromq 问题
    无论是在windows上编译,还是linux上编译,zeromq都报了很多错误,zeromq 是编译成可执行文件的形式,被zerorpc调用的,
    经常会遇到“A dynamic link library (DLL) initialization routine failed.” 或"zmq.node: undefined symbol" 这种错误,
    一般就是缺少依赖库了。
    在zeromq 安装目录,会有Readme文件,里面会有一些相关说明,可以通过查阅该文件,提供一些思路。
If you want to use `zeromq` inside your [Electron](http://electron.atom.io/) application
it needs to be rebuild against Electron headers. We ship prebuilt binaries for Electron so you won't need to build `zeromq` from source.

You can rebuild `zeromq` manually by running:
```bash
npm rebuild zeromq --runtime=electron --target=1.4.5

Where target is your desired Electron version. This will download the correct binary for usage in Electron.

系统的Electron用的是3.1.13版本, 但是rebuild是,能找到的target版本,最大就是3.1.0版本,所以就会用到上面提的Native Node Module的方法。

npm rebuild zeromq --runtime=electron --target=3.1.0

./node_modules/.bin/electron-rebuild

需要注意的问题

  1. npm所使用的python版本
    系统中安装的python版本是3.6, 而npm只能使用python2, 所以需要配置npm的python版本,npm对应的配置文件是npmrc

通过"npm config ls" 查看npm的安装路径,新建etc目录,创建npmrc文件。

python=F:\Python27\python.exe

也可以通过命令,修改配置:

npm config set python python2.7
  1. Nodejs, Python通信可以采用的方式

    zerorpc

    request-promise

    python-shell

    stdin.write()

参考:

https://github.com/electron/electron/blob/master/docs/tutorial/using-native-node-modules.md
https://github.com/zeromq/zeromq.js#rebuilding-for-electron

https://github.com/fyears/electron-python-example
https://github.com/fyears/electron-python-example/blob/master/old-post-backup/README.md

https://www.sohamkamani.com/blog/2015/08/21/python-nodejs-comm/
https://www.quora.com/How-do-I-send-data-from-Node-js-to-Python-and-get-back-JSONN-data-from-Python-to-Node-jss

https://zhuanlan.zhihu.com/p/37999476
https://mlln.cn/2018/01/08/electron%E4%BD%9C%E4%B8%BApython%E7%95%8C%E9%9D%A2%E5%BC%80%E5%8F%91%E5%85%A5%E9%97%A8/

https://www.npmjs.com/package/request-promise
https://www.techiediaries.com/python-electron-tutorial/
https://blog.csdn.net/duanaibing3017/article/details/84099268

https://medium.com/@abulka/electron-python-4e8c807bfa5e

https://nodejs.org/zh-cn/download/releases/

https://blog.csdn.net/jbguo/article/details/90701266
https://www.kancloud.cn/idcpj/python/1012428
https://blog.csdn.net/u014563989/article/details/78013804
https://stackoverflow.com/questions/32158738/python-on-electron-framework

https://electronjs.org/docs

https://blog.csdn.net/jishuqianjin/article/details/81778407

猜你喜欢

转载自blog.csdn.net/wwlhz/article/details/100116644