📜  yarn install python2 not found - Javascript (1)

📅  最后修改于: 2023-12-03 15:06:06.006000             🧑  作者: Mango

Yarn Install: Python2 Not Found - 解决方案

在使用 yarn install 命令的时候,你可能会碰到类似于这样的错误信息:

$ yarn install
yarn install v1.15.2
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
error An unexpected error occurred: "Command failed.
Exit code: 1
Command: sh
Arguments: -c node-gyp rebuild
Directory: /Users/username/code/node_modules/node-sass
Output:
gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@11.2.0 | darwin | x64
gyp info spawn /usr/bin/python2
gyp: Call to 'node -e "console.log('\''running'\'' , '\''node-gyp configure'\'')"'
returned exit status 0 while in binding.gyp. while trying to load binding.gyp
gyp ERR! configure error 
gyp ERR! stack Error: Can't find Python executable "/usr/bin/python2", you can set the PYTHON
executable path with the PYTHON env variable.
gyp ERR! stack     at PythonFinder.failNoPython (/usr/local/Cellar/node/11.2.0/libexec 
/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:483:19)
gyp ERR! stack     at PythonFinder.<anonymous> (/usr/local/Cellar/node/11.2.0/libexec/lib/
 node_modules/npm/node_modules/node-gyp/lib/configure.js:508:16)
gyp ERR! stack     at /usr/local/Cellar/node/11.2.0/libexec/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:484:16
gyp ERR! stack     at ChildProcess.exithandler (child_process.js:294:5)
gyp ERR! stack     at ChildProcess.emit (events.js:182:13)
gyp ERR! stack     at maybeClose (internal/child_process.js:962:16)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
gyp ERR! System Darwin 18.0.0
gyp ERR! command "/usr/local/Cellar/node/11.2.0/bin/node" "/usr/local/Cellar/
node/11.2.0/libexec/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" 
"rebuild"
gyp ERR! cwd /Users/username/code/node_modules/node-sass
gyp ERR! node -v v11.2.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

这个错误信息实际上是在你的安装过程中出现了问题,很可能是环境变量没有设置好,或者缺少依赖项。其中提到了 Python2 找不到,这就是问题的根源。

下面是一些解决方法:

1. 安装Python2

首先,你需要确保你的系统中安装了 Python2。你可以使用以下命令进行检查:

python --version

如果你安装了Python2,你应该会看到类似下面的输出:

Python 2.x.x

如果你没有安装Python2,可以从官网上下载:

安装好之后,你需要将Python2的可执行文件路径添加到环境变量中。例如,在MacOS中,你可以在.bash_profile文件中添加:

export PATH="/usr/bin:$PATH"

保存之后,你可以执行以下命令来应用更改:

source ~/.bash_profile
2. 安装node-gyp

node-gyp 是一个用来编译和安装npm包的工具,它依赖于Python2。你可以通过以下命令来安装它:

npm install -g node-gyp

如果你已经安装了,可以尝试升级:

npm update -g node-gyp
3. 使用npm安装

如果通过yarn安装遇到了问题,你可以尝试用npm来安装:

npm install

总的来说,这个错误信息是因为缺少Python2,你需要确保你的系统中安装了它,并将其添加到环境变量中。另外,你还需要确保安装了node-gyp,或者尝试使用npm安装。

以上内容仅供参考。