0

ENVIRONMENT : Windows 10 & Python 3.7

I am trying to test if some programs are installed on a computer with Python 3.7

I am using this code:

print(f"p_command:{p_command}")
proc = subprocess.Popen(p_command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, close_fds=True)
returncode = proc.wait()
print(f'returncode : {returncode} - {type(returncode)}')
output = proc.stdout.read()

try:
   output = output.decode('utf-8')
except Exception as ex:
   logger.error(f"ERROR with TestIfInstalled => output = output.decode('utf-8'): {ex} ")

When I try to see if "node" is installed with the command (p_command) : "node --version" but it bugs! This is the output:

p_command:node --version
returncode : 1 - <class 'int'>
ERROR with TestIfInstalled => output = output.decode('utf-8'): 'utf-8' codec can't decode byte 0x82 in position 82: invalid start byte 
output : b"'node' is not recognized as an internal command\r\nou externe, un programme ex\x82cutable ou un fichier de commandes.\r\n" returncode : 1

So I tested from my terminal by typing manually node --version" and I get correct answer because it is installed and it path is in environment variable PATH:

C:\Users\gauth>node --version
v12.15.0

C:\Users\gauth>

This is the content of PATH:

PATH=C:\Program Files\Common Files\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\windows\System32\OpenSSH\;C:
\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\wamp64\bin\php\php7.4.9;C:\Program Files\PuTTY\;C:\MinGW64\bin;C:\Qt\QtIFW-4.1.0;C:\Program Files\Git\cmd;C:\Windows\System32;C:\Pro
gram Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files (x86)\Windows Kits\10\bin\x64;C:\Program Files (x86)\nodejs\;C:\Users\gauth\AppData\Local\Android\cmdline-to
ols\tools\bin;C:\Users\gauth\AppData\Local\Android\platform-tools;C:\Users\gauth\AppData\Local\Android;C:\Users\gauth\AppData\Local\Android\build-tools\28.0.3;C:\Program Files\Tesseract-O
CR\tessdata;C:\Program Files\Tesseract-OCR;C:\Program Files (x86)\nodejs\node_modules\npm\bin;C:\Users\gauth\AppData\Local\Android\tools\lib\x86_64;C:\Users\gauth\AppData\Local\Android\to
ols;C:\Users\gauth\AppData\Local\Programs\Python\Python37\Scripts\;C:\Users\gauth\AppData\Local\Programs\Python\Python37\;C:\Users\gauth\AppData\Local\Microsoft\WindowsApps;C:\Users\gauth
\AppData\Local\Programs\Microsoft VS Code\bin;C:\Program Files\JetBrains\PyCharm Community Edition 2021.1.1\bin;C:\Users\gauth\AppData\Roaming\npm;C:\Users\gauth\AppData\Local\GitHubDeskt
op\bin

So as a result, I suppose my Python script is not loading properly in the PATH environment? How to force python to load all the values from PATH in memory in order to execute properly the command "node --version"? Can you help, please?

WHAT DID I TRY?

I tried to print the value of "PATH" from my python script:

Path = os.getenv('Path')
print(f"Path:{Path}")

OUTPUT:

Path:C:\Users\gauth\AppData\Local\Programs\Python\Python37\lib\site-packages\cv2\../../x64/vc14/bin;C:\Users\gauth\AppData\Local\Programs\Python\Python37\lib\site-packages\PyQt5\Qt5\bin;C:\Users\gauth\AppData\Local\Programs\Python\Python37\lib\site-packages\pywin32_system32;C:\Program Files\Common Files\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\windows\System32\OpenSSH\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\wamp64\bin\php\php7.4.9;C:\Program Files\PuTTY\;C:\MinGW64\bin;C:\Qt\QtIFW-4.1.0;C:\Program Files\Git\cmd;C:\Windows\System32;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files (x86)\Windows Kits\10\bin\x64;C:\Program Files (x86)\nodejs\;C:\Users\gauth\AppData\Local\Android\cmdline-tools\tools\bin;C:\Users\gauth\AppData\Local\Android\platform-tools;C:\Users\gauth\AppData\Local\Android;C:\Users\gauth\AppData\Local\Android\build-tools\28.0.3;C:\Program Files\Tesseract-OCR\tessdata;C:\Program Files\Tesseract-OCR;C:\Program Files (x86)\nodejs\node_modules\npm\bin;C:\Users\gauth\AppData\Local\Android\tools\lib\x86_64;C:\Users\gauth\AppData\Local\Android\tools;C:\Users\gauth\AppData\Local\Programs\Python\Python37\Scripts\;C:\Users\gauth\AppData\Local\Programs\Python\Python37\;C:\Users\gauth\AppData\Local\Microsoft\WindowsApps;C:\Users\gauth\AppData\Local\Programs\Microsoft VS Code\bin;C:\Program Files\JetBrains\PyCharm Community Edition 2021.1.1\bin;C:\Users\gauth\AppData\Roaming\npm;C:\Users\gauth\AppData\Local\GitHubDesktop\bin;C:\Users\gauth\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\.libs

As you can see, there is the node path in it.

Gauthier Buttez
  • 1,005
  • 1
  • 16
  • 40
  • Why would `Path` somehow have the right PATH while `PATH` has the wrong one? If you ran `print(os.environ['PATH'])` _before_ changing it in your Python script, that would do a lot of good to demonstrate that the question is trying to solve the correct problem. – Charles Duffy Apr 03 '22 at 12:27
  • related: https://stackoverflow.com/questions/5226958/how-can-i-find-the-path-for-an-executable – OrenIshShalom Apr 03 '22 at 12:30
  • Thank for your help. i did and update the question with the output. – Gauthier Buttez Apr 03 '22 at 12:30
  • The edit to the question says it's showing the output of dumping `Path`, not the output of dumping the original `PATH`. I'm asking to see the purportedly-incorrect original `PATH`, not the `Path`. – Charles Duffy Apr 03 '22 at 12:31
  • (as what you show with the header "*This is the content of PATH*" already contains `C:\Program Files (x86)\nodejs\`, so if that's to be taken at its word your PATH is already correct and doesn't need to be "fixed"). – Charles Duffy Apr 03 '22 at 12:34
  • Thanks @CharlesDuffy; so why Python can't execute this command "node --version" properly? Why it returns error code "1" instead of "0"? – Gauthier Buttez Apr 03 '22 at 12:36
  • The _best-practice_ way to do this would be to take out the `shell=True` and switch to something like `command_pieces = shlex.split(p_command); command_exe = shutil.which(command_pieces[0]); command_args = command_pieces[1:]; proc = subprocess.Popen([command_exe]+command_pieces, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)` -- one advantage is that if your executable isn't in the PATH, `shutil.which` will give you an unambiguous exception saying so. – Charles Duffy Apr 03 '22 at 12:37
  • At this point, I don't have an answer for why, I'm just trying to encourage that you avoid assuming you already know why it's happening yourself and only looking for answers that fit that assumption, as seems to be the case. It may turn out the assumption is right, but I wouldn't consider it _known_ without further evidence. – Charles Duffy Apr 03 '22 at 12:37
  • BTW -- is this a native Windows Python executable? A Cygwin Python executable? Something else? – Charles Duffy Apr 03 '22 at 12:39
  • One other thing: Try logging the `repr()` of your `p_command` so we can see if it contains nonprintable characters. Right now you're logging the `str()` form of that string, so it could have a character the terminal silently ignores and we'd have no way of knowing. – Charles Duffy Apr 03 '22 at 12:41
  • I did print(f"p_command:{repr(p_command)}") and the output is p_command:'node --version'. Nothing weird. – Gauthier Buttez Apr 03 '22 at 12:44
  • TO the question "native Windows Python executable" ? it is executed from Pycharm with this python "C:\Users\gauth\AppData\Local\Programs\Python\Python37\python.exe" – Gauthier Buttez Apr 03 '22 at 12:45
  • I executed the script from my windows Terminal (instead of Pycharm) and it gives the same result. – Gauthier Buttez Apr 03 '22 at 12:48
  • ...okay, and `shutil.which()` find the `node` executable? (It's explicitly doing a PATH lookup, so if it works we know the PATH is fine). BTW, it might be necessary to switch to `node.exe` -- I don't know if `shutil` handles searching extensions on Windows. – Charles Duffy Apr 03 '22 at 13:03

0 Answers0