当前位置: 首页 > 工具软件 > RobotJS > 使用案例 >

windows安装robotjs的心路

尉迟招
2023-12-01

使用npm安装robotjs所遇到的问题

问题

通过命令npm install robotjs --save进行安装,遇到第一个待解决问题

gyp ERR! find Python 
gyp ERR! find Python Python is not set from command line or npm configuration
gyp ERR! find Python Python is not set from environment variable PYTHON
gyp ERR! find Python checking if "python" can be used
gyp ERR! find Python - "python" is not in PATH or produced an error
gyp ERR! find Python checking if "python2" can be used
gyp ERR! find Python - "python2" is not in PATH or produced an error
gyp ERR! find Python checking if "python3" can be used
gyp ERR! find Python - "python3" is not in PATH or produced an error
gyp ERR! find Python checking if the py launcher can be used to find Python 2
gyp ERR! find Python - "py.exe" is not in PATH or produced an error
gyp ERR! find Python checking if Python is C:\Python27\python.exe
gyp ERR! find Python - "C:\Python27\python.exe" could not be run
gyp ERR! find Python checking if Python is C:\Python37\python.exe
gyp ERR! find Python - "C:\Python37\python.exe" could not be run
gyp ERR! find Python
gyp ERR! find Python **********************************************************
gyp ERR! find Python You need to install the latest version of Python.
gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
gyp ERR! find Python you can try one of the following options:
gyp ERR! find Python - Use the switch --python="C:\Path\To\python.exe"
gyp ERR! find Python   (accepted by both node-gyp and npm)
gyp ERR! find Python - Set the environment variable PYTHON
gyp ERR! find Python - Set the npm configuration variable python:
gyp ERR! find Python   npm config set python "C:\Path\To\python.exe"
gyp ERR! find Python For more information consult the documentation at:
gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
gyp ERR! find Python **********************************************************
gyp ERR! find Python
gyp ERR! configure error
gyp ERR! stack Error: Could not find any Python installation to use
gyp ERR! stack     at PythonFinder.fail (C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\node-gyp\lib\find-python.js:307:47)
gyp ERR! stack     at PythonFinder.runChecks (C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\node-gyp\lib\find-python.js:136:21)
gyp ERR! stack     at PythonFinder.<anonymous> (C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\node-gyp\lib\find-python.js:225:16)
gyp ERR! stack     at PythonFinder.execFileCallback (C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\node-gyp\lib\find-python.js:271:16)
gyp ERR! stack     at exithandler (child_process.js:315:5)
gyp ERR! stack     at ChildProcess.errorhandler (child_process.js:327:5)
gyp ERR! stack     at ChildProcess.emit (events.js:314:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:274:12)
gyp ERR! stack     at onErrorNT (internal/child_process.js:464:16)
gyp ERR! stack     at processTicksAndRejections (internal/process/task_queues.js:80:21)
gyp ERR! System Windows_NT 10.0.18363
gyp ERR! command "C:\\dev\\nodejs\\node.exe" "C:\\dev\\nvm\\v14.13.1\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"      
gyp ERR! cwd D:\新建文件夹\代码\VSCode\Electron\RemoteControl\app\renderer\src\remotecontrol\node_modules\robotjs
gyp ERR! node -v v14.13.1
gyp ERR! node-gyp -v v5.1.0
gyp ERR! not ok

搜索结果:

结果一:npm install --global --production windows-build-tools
结果二:手动安装python2.7版本,配置环境变量

二者本质上应该就是安装python,并配置环境变量
结果一 会一直卡死在 python 安装成功上
具体原因不详

继续入坑

  1. 手动安装python成功
  2. 先使用npm cache clean -f命令清理npm缓存,尽量排除掉干扰
  3. 再次使用npm install robotjs --save进行安装,至此遇到第二个问题
Traceback (most recent call last):
  File "C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\node-gyp\gyp\gyp_main.py", line 50, in <module>
    sys.exit(gyp.script_main())
  File "C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 554, in script_main
    return main(sys.argv[1:])
  File "C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 547, in main
    return gyp_main(args)
  File "C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 532, in gyp_main
    generator.GenerateOutput(flat_list, targets, data, params)
  File "C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\node-gyp\gyp\pylib\gyp\generator\msvs.py", line 2008, in GenerateOutput
    msvs_version)
  File "C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\node-gyp\gyp\pylib\gyp\generator\msvs.py", line 1843, in _CreateProjectObjects
    guid = _GetGuidOfProject(proj_path, spec)
  File "C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\node-gyp\gyp\pylib\gyp\generator\msvs.py", line 910, in _GetGuidOfProject
    guid = guid or MSVSNew.MakeGuid(proj_path)
  File "C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\node-gyp\gyp\pylib\gyp\MSVSNew.py", line 48, in MakeGuid
    d = hashlib.md5((str(seed) + str(name)).encode('utf-8')).hexdigest().upper()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 11: ordinal not in range(128)
gyp ERR! configure error 
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\node-gyp\lib\configure.js:351:16)
gyp ERR! stack     at ChildProcess.emit (events.js:314:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)
gyp ERR! System Windows_NT 10.0.18363
gyp ERR! command "C:\\dev\\nodejs\\node.exe" "C:\\dev\\nvm\\v14.13.1\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"      
gyp ERR! cwd D:\新建文件夹\代码\VSCode\Electron\RemoteControl\app\renderer\src\remotecontrol\node_modules\robotjs
gyp ERR! node -v v14.13.1
gyp ERR! node-gyp -v v5.1.0
gyp ERR! not ok

经过搜索以及提示排查
大致问题是ascii的编码问题
找到提示的第一个文件File "C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\node-gyp\gyp\gyp_main.py", line 50, in <module>

出坑

在找到的文件中修改如下

import os
import sys
import subprocess

# 添加代码
# 由于windows默认是使用的gbk字符集
# 故此将字符集修改为gbk
defaultencoding = 'gbk'
if sys.getdefaultencoding() != defaultencoding:
    reload(sys)
    sys.setdefaultencoding(defaultencoding)
# 结束添加代码

PY3 = bytes != str

# Below IsCygwin() function copied from pylib/gyp/common.py
def IsCygwin():
  try:
    out = subprocess.Popen("uname",
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT)
    stdout, stderr = out.communicate()
    if PY3:
      stdout = stdout.decode("utf-8")
    return "CYGWIN" in str(stdout)
  except Exception:
    return False

成功

再次使用npm install robotjs --save进行安装
输出如下:

> robotjs@0.6.0 install D:\新建文件夹\代码\VSCode\Electron\RemoteControl\app\renderer\src\remotecontrol\node_modules\robotjs
> prebuild-install || node-gyp rebuild

prebuild-install WARN install No prebuilt binaries found (target=14.13.1 runtime=node arch=x64 libc= platform=win32)

D:\新建文件夹\代码\VSCode\Electron\RemoteControl\app\renderer\src\remotecontrol\node_modules\robotjs>if not defined npm_config_node_gyp (node "C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "C:\dev\nvm\v14.13.1\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" rebuild )
在此解决方案中一次生成一个项目。若要启用并行生成,请添加“-m”开关。
  robotjs.cc
  win_delay_load_hook.cc
D:\新建文件夹\代码\VSCode\Electron\RemoteControl\app\renderer\src\remotecontrol\node_modules\robotjs\src\os.h(26): warning C4005: “STRICT”: 宏重定
义 (编译源文件
..\src\robotjs.cc) [D:\新建文件夹\代码\VSCode\Electron\RemoteControl\app\renderer\src\remotecontrol\node_modules\robotjs\build\robotjs.vcxproj]
  C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\shared\minwindef.h(23): note: 参见“STRICT”的前一个定义 (编译源文件 ..\src\robotjs.cc)
  deadbeef_rand.c
  mouse.c
  keypress.c
  keycode.c
  screen.c
  screengrab.c
  snprintf.c
  MMBitmap.c
    正在创建库 D:\新建文件夹\代码\VSCode\Electron\RemoteControl\app\renderer\src\remotecontrol\node_modules\robotjs\build\Release\robotjs.lib 和对
象 D:\新建文件夹\代码
  \VSCode\Electron\RemoteControl\app\renderer\src\remotecontrol\node_modules\robotjs\build\Release\robotjs.exp
  robotjs.vcxproj -> D:\新建文件夹\代码\VSCode\Electron\RemoteControl\app\renderer\src\remotecontrol\node_modules\robotjs\build\Release\\robotjs.node
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: iconv-corefoundation@1.1.5 (node_modules\iconv-corefoundation):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for iconv-corefoundation@1.1.5: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\watchpack-chokidar2\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\webpack-dev-server\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: dmg-license@1.0.8 (node_modules\dmg-license):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for dmg-license@1.0.8: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

+ robotjs@0.6.0
added 40 packages from 33 contributors in 75.885s

107 packages are looking for funding
  run `npm fund` for details

再遇问题

  1. robotjs安装成功后,还需要进行编译的一个过程
  2. 先使用npm install electron-rebuild --save-dev命令安装编译所需的electron-rebuild
  3. 使用npx electron-rebuild进行编译,然而问题又一次的出现了
- Searching dependency tree
gyp info find Python using Python version 2.7.15 found at "C:\Python27\python.exe"
gyp http GET https://www.electronjs.org/headers/v11.4.4/node-v11.4.4-headers.tar.gz
gyp WARN install got an error, rolling back install
✖ Rebuild Failed

An unhandled error occurred inside electron-rebuild
node-gyp failed to rebuild 'D:\新建文件夹\代码\VSCode\Electron\RemoteControl\app\renderer\src\remotecontrol\node_modules\robotjs'.
Error: getaddrinfo EAI_AGAIN gh-contractor-zcbenz.s3.amazonaws.com


Error: node-gyp failed to rebuild 'D:\新建文件夹\代码\VSCode\Electron\RemoteControl\app\renderer\src\remotecontrol\node_modules\robotjs'.
Error: getaddrinfo EAI_AGAIN gh-contractor-zcbenz.s3.amazonaws.com


    at ModuleRebuilder.rebuildNodeGypModule (D:\新建文件夹\代码\VSCode\Electron\RemoteControl\app\renderer\src\remotecontrol\node_modules\electron-rebuild\lib\src\module-rebuilder.js:193:19)
    at async Rebuilder.rebuildModuleAt (D:\新建文件夹\代码\VSCode\Electron\RemoteControl\app\renderer\src\remotecontrol\node_modules\electron-rebuild\lib\src\rebuild.js:190:9)
    at async Rebuilder.rebuild (D:\新建文件夹\代码\VSCode\Electron\RemoteControl\app\renderer\src\remotecontrol\node_modules\electron-rebuild\lib\src\rebuild.js:152:17)
    at async D:\新建文件夹\代码\VSCode\Electron\RemoteControl\app\renderer\src\remotecontrol\node_modules\electron-rebuild\lib\src\cli.js:146:9
  1. 使用命令npx electron-rebuild -d=https://npm.taobao.org/mirrors/atom-shell尝试使用淘宝镜像进行解决,but 依然报错,而且似乎更严重
  2. 根据关键字gyp WARN install got an error, rolling back install进行搜索
  3. 结果:需要设置(临时的)环境变量,以使得其能重定向

详情参见:https://blog.csdn.net/u013613428/article/details/51545479
由此去

  1. 使用set ATOM_NODE_URL=http://gh-contractor-zcbenz.s3.amazonaws.com/atom-shell/dist进行设置环境变量,完成其重定向
  2. 再使用npx electron-rebuild命令进行编译,but又有问题,但一看便知与上面的字符编码是同一个问题,故此便不再赘述
 类似资料: