Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that python.exe is actually of version 2 #987

Closed
neico opened this issue Jul 12, 2016 · 8 comments
Closed

Make sure that python.exe is actually of version 2 #987

neico opened this issue Jul 12, 2016 · 8 comments

Comments

@neico
Copy link

neico commented Jul 12, 2016

On my windows machine I've got the python launcher (py.exe) including python3 by default inside my PATH. this results in gyp assuming that the python.exe found is ok and runs into a version mismatch afterwards.

What I did to work around this is to ensure in checkPython() that if there's a python.exe, it'll most likely also have 'python2' in it's path somewhere, if it doesn't, handle it as if which failed by changing lib/configure.js:344 to this:

      // never trust the python env alone
      if (err || !execPath.toLowerCase().includes('python2')) {

This is not perfect as it relies on at least one ES6 function and I think there might be a more elegant method, maybe letting checkPythonVersion() actually return the version and work out based on that if that executable should be used.

That's why I didn't put this up as a PR but as a issue for someone to figure out the best course of action.

For me, after changing this line the new checkPythonLauncher() resulted in my setup working fine afterwards so I'm going to live with that.

@bnoordhuis
Copy link
Member

As a workaround, it sounds like node-gyp --python=c:\path\to\python2.exe rebuild should take care of that.

I don't quite understand why it's not working for you out of the box, though. node-gyp looks for python2.exe first before it consults the python launcher, unless you pass --python= or the PYTHON environment variable is set.

@neico
Copy link
Author

neico commented Jul 12, 2016

Well there's no python related env variable set here, and I'm not passing --python (or told npm to use a different path for that matter) so according to lib/configure.js:30 it should work like that, yes, but it doesn't, it's somehow fetching the python3 variant instead (might be because on the windows versions there's only python.exe, no python3.exe and no python2.exe, which is why py.exe most likely exists in the first place, what does exist are python3.dll etc.)

I'd probably suggest checking for the launcher before anything else but seeing that it's not that simple to "parse" the launcher it might be quite some overhead by doing that.

I haven't really checked what exactly which is doing there, but seeing that my workaround works let's me assume that it's able to get python.exe trough "python2", but since that doesn't exist it might be trying to find the next best thing and is finding the python3 executable in turn...

Just for the record:
py.exe is located in C:\Windows,
while my python3 and python2 are located in C:\ProgramFiles respectively
and that C:\Windows as well as C:\ProgramFiles\Python3 are present in PATH. (so that applications starting python.exe directly by default get v3 and not a missing executable error)

@bnoordhuis
Copy link
Member

Okay, I think that explains it. node-gyp first looks for python2.{exe,bat,cmd}. If that fails, it tries python.{exe,bat,cmd} and only then does it fall back to the python launcher.

On your system, it's picking up (and rejecting) the python3 python.exe when it could still fall back to the python launcher. I'll tweak the logic.

@emigenix
Copy link

emigenix commented Aug 24, 2016

The way the python check is currently implemented in ../node_modules/node-gyp/lib/configure.js it fails on machines where: (a) python is soft linked to an .exe, .cmd, bat and (b) where the Windows PYTHON environment variable is set to something not in root C: etc. Needless to say, this fails in most other conditions. For example for Cygwin or W10 bash users...

@neico
Copy link
Author

neico commented Aug 24, 2016

@emigenix Did you check the PR? does that solve it for you?

You can test it by using npm install -g bnoordhuis/node-gyp#fix987

@emigenix
Copy link

@neico Actually that fixed the installation error of node-gyp, but the python related errors remain for other packages (referencing node-gyp). E.g. node-red, which gives:

$ npm install -g node-red
npm WARN deprecated [email protected]: you can use npm install i18next from version 2.0.0
npm WARN deprecated [email protected]: ReDoS vulnerability parsing Set-Cookie https://nodesecurity.io/advisories/130
C:\Users\xxx\AppData\Roaming\npm\node-red -> C:\Users\xxx\AppData\Roaming\npm\node_modules\node-red\red.js
C:\Users\xxx\AppData\Roaming\npm\node-red-pi -> C:\Users\xxx\AppData\Roaming\npm\node_modules\node-red\bin\node-red-pi

> [email protected] install C:\Users\xxx\AppData\Roaming\npm\node_modules\node-red\node_modules\bufferutil
> node-gyp rebuild


C:\Users\xxx\AppData\Roaming\npm\node_modules\node-red\node_modules\bufferutil>if not defined npm_config_node_gyp (node "C:\mybin\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "" rebuild )
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "C:\cygwin64\bin\python", you can set the PYTHON env variable.
gyp ERR! stack     at failNoPython (C:\mybin\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:401:14)
gyp ERR! stack     at C:\mybin\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:356:11
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:123:15)
gyp ERR! System Windows_NT 6.3.9600
gyp ERR! command "C:\\mybin\\nodejs\\node.exe" "C:\\mybin\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\xxx\AppData\Roaming\npm\node_modules\node-red\node_modules\bufferutil
gyp ERR! node -v v6.4.0
gyp ERR! node-gyp -v v3.3.1
gyp ERR! not ok
npm WARN install:[email protected] [email protected] install: `node-gyp rebuild`
npm WARN install:[email protected] Exit status 1

> [email protected] install C:\Users\xxx\AppData\Roaming\npm\node_modules\node-red\node_modules\utf-8-validate
> node-gyp rebuild


C:\Users\xxx\AppData\Roaming\npm\node_modules\node-red\node_modules\utf-8-validate>if not defined npm_config_node_gyp (node "C:\mybin\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "" rebuild )
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "C:\cygwin64\bin\python", you can set the PYTHON env variable.
gyp ERR! stack     at failNoPython (C:\mybin\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:401:14)
gyp ERR! stack     at C:\mybin\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:356:11
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:123:15)
gyp ERR! System Windows_NT 6.3.9600
gyp ERR! command "C:\\mybin\\nodejs\\node.exe" "C:\\mybin\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\xxx\AppData\Roaming\npm\node_modules\node-red\node_modules\utf-8-validate
gyp ERR! node -v v6.4.0
gyp ERR! node-gyp -v v3.3.1
gyp ERR! not ok
npm WARN install:[email protected] [email protected] install: `node-gyp rebuild`
npm WARN install:[email protected] Exit status 1
...
[etc]

@emigenix
Copy link

emigenix commented Aug 24, 2016

@neico I've fixed that error! It's due to how Cygwin treats symbolic links. It doesn't do that properly in an out-of-the box installation. So the error messages above become misleading, as it complains about the PYTHON path and not the existence of an python.exe file itself.

The resolution for out-of-the-box Cygwin users is this:

# Open a native Windows CMD in Administrator mode and:
cd C:\cygwin64\bin\
mklink python.exe python2.7.exe

The result should look like this:

C:\cygwin64\bin>ls -al python*
lrwxrwxrwx 1 xxx            xxx   13 Jun  2  2015 python -> python2.7.exe
lrwxrwxrwx 1 xxx            xxx   16 Jun  2  2015 python-config -> python2.7-config
lrwxrwxrwx 1 Administrators xxx   13 Aug 24 17:28 python.exe -> python2.7.exe
lrwxrwxrwx 1 xxx            xxx   13 Jun  2  2015 python2 -> python2.7.exe
lrwxrwxrwx 1 xxx            xxx   16 Jun  2  2015 python2-config -> python2.7-config
-rwxr-xr-x 1 xxx            xxx 1685 Jun  2  2015 python2.7-config
-rwxr-xr-x 1 xxx            xxx 9235 Jun  2  2015 python2.7.exe

Therefore the node-gyp --python instructions doesn't work.

@bnoordhuis Can you guys please update your installation README info for Windows/Cygwin users?

EDIT I posted a full answer here.

@SatishMHiremath
Copy link

I have faced and fixed this issue relates node-sass and python error.Try installing latest angular cli command with the steps below

Set the system variable path as well:
C:\Users\windowsusername\AppData\Roaming\npm

C:\Users\windowsusername\AppData\Roaming\npm\node_modules@angular\cli\bin;

C:\Program Files\nodejs;

Then start using angular cli command prompt using:
angular client project folder path>npm install -g @latest

angular client project folder path>npm install -g node-gyp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants