Node. js is built with GYP — cross-platform built tool written in Python. Also some other build steps are implemented in Python. So Python is required for building node from source.
js and Python finding out that Node. js is awesome for Web Development and Python for Data Sciences. Actually, we don't need to always stick with the same programming language as there are ways to use them both together. In this article, I will show you an example of how to use a Python script from the Node.
NPM has a package called windows-build-tools that should automatically install everything you need to get node-gyp working, including the Microsoft build tools, compilers, Python, and everything else required to build native Node modules on Windows.
To Solve Can't find Python executable “python”, you can set the PYTHON env variable Error You just need to Install Python In Your Windows ( If Not Installed ) and then add python to your PATH variable. Using environment variable. OR You can set npm config set python path and it will also Solve your error.
If you haven't got python installed along with all the node-gyp dependencies, simply open Powershell or Git Bash with administrator privileges and execute:
npm install --global --production windows-build-tools
and then to install the package:
npm install --global node-gyp
once installed, you will have all the node-gyp dependencies downloaded, but you still need the environment variable. Validate Python is indeed found in the correct folder:
C:\Users\ben\.windows-build-tools\python27\python.exe
*Note - it uses python 2.7 not 3.x as it is not supported*
If it doesn't moan, go ahead and create your (user) environment variable:
setx PYTHON "%USERPROFILE%\.windows-build-tools\python27\python.exe"
restart cmd, and verify the variable exists via set PYTHON
which should return the variable ($env:PYTHON
if using Powershell)
Lastly re-apply npm install <module>
Your problem is that you didn't set the environment variable.
The error clearly says this:
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
And in your comment, you say you did this:
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
That's nice, but that doesn't set the PYTHON
variable, it sets the PYTHONPATH
variable.
Meanwhile, just using the set
command only affects the current cmd
session. If you reboot after that, as you say you did, you end up with a whole new cmd
session that doesn't have that variable set in it.
There are a few ways to set environment variables permanently—the easiest is in the System Control Panel in XP, which is of course different in Vista, different again in 7, and different again in 8, but you can google for it.
Alternatively, just do the set
right before the npm
command, without rebooting in between.
You can test whether you've done things right by doing the exact same thing the config script is trying to do: Before running npm
, try running %PYTHON%
. If you've done it right, you'll get a Python interpreter (which you can immediately quit). If you get an error, you haven't done it right.
There are two problems with this:
set PYTHON=%PYTHON%;D:\Python
First, you're setting PYTHON
to ;D:\Python
. That extra semicolon is fine for a semicolon-separated list of paths, like PATH
or PYTHONPATH
, but not for a single value like PYTHON
. And likewise, adding a new value to the existing value is what you want when you want to add another path to a list of paths, but not for a single value. So, you just want set PYTHON=D:\Python
.
Second, D:\Python
is not the path to your Python interpreter. It's something like D:\Python\Python.exe
, or D:\Python\bin\Python.exe
. Find the right path, make sure it works on its own (e.g., type D:\Python\bin\Python.exe
and make sure you get a Python interpreter), then set the variable and use it.
So:
set PYTHON=D:\Python\bin\Python.exe
Or, if you want to make it permanent, do the equivalent in the Control Panel.
For me after installing windows-build-tools with the below comment
npm --add-python-to-path='true' --debug install --global windows-build-tools
running the code below
npm config set python "%USERPROFILE%\.windows-build-tools\python27\python.exe"
has worked.
Here is a guide that resolved a lot of these issues for me.
http://www.steveworkman.com/node-js/2012/installing-jsdom-on-windows/
I remember in particular the python version as important. Make sure you install 2.7.3 instead of 3's.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With