2

Okay so my best guess currently is that I've severely screwed up my python environment somehow and I have no idea how.

First off, I have tried uninstalling and reinstalling (actually upgrading from 2.7.14 to .15) python to no avail. the problem persists.

The problem description as best as I understand it:

I am trying to call a Windows native binary from Python 2.7.15 (displayswitch.exe, which is verified existing in C:\Windows\System32\). Note: I didn't miss the double slashes, stack overflow is taking away the trailing ones for some reason. Probably an escape sequence.

At one point this was working just fine, which leads me to believe I screwed something up. As of this moment, I am able to call displayswitch, and any other native command just fine from a windows command prompt, but running the exact same command from os.system, subprocess.call gives me the errors "displayswitch is not recognized as an internal or external command" and "WindowsError: The system cannot find the file specified", respectively.

Here are snippets of exactly what I'm running, copied from the command line for sanity reasons:

(dos)

displayswitch.exe --> runs fine

(python interpreter)

import os
os.system("displayswitch.exe")
--> 'displayswitch' is not recognized as an internal or external command,
    operable program or batch file.
    1

(python interpreter)

import subprocess
subprocess.call(["displayswitch.exe"])
--> WindowsError: [Error 2] The system cannot find the file specified

My first instinct was that I had screwed up my path variable (which again is a long shot because I haven't touched it with a 30 foot pole), but if i print out os.environ["PATH"], the path to C:\Windows\System32 is in there (i split it on semicolons so it'd be easier to see):

C:\\Python27\\;
C:\\Python27\\Scripts;
C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;
C:\\Program Files (x86)\\Razer Chroma SDK\\bin;
C:\\Program Files\\Razer Chroma SDK\\bin;
C:\\Windows\\system32;
C:\\Windows;
C:\\Windows\\System32\\Wbem;
C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;
C:\\Program Files\\PuTTY\\;
C:\\WINDOWS\\system32;
C:\\WINDOWS;
C:\\WINDOWS\\System32\\Wbem;
C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;
C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;
C:\\Program Files\\TortoiseSVN\\bin;
C:\\Users\\iwasf\\AppData\\Local\\Microsoft\\WindowsApps;
C:\\sqlite;
C:\\Program Files (x86)\\WinMerge;
C:\\WINDOWS\\system32;
C:\\WINDOWS;
C:\\WINDOWS\\System32\\Wbem;
C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;
C:\\WINDOWS\\System32\\OpenSSH\\;
C:\\Program Files\\Git\\cmd;
C:\\Program Files\\Git\\mingw64\\bin;
C:\\Program Files\\Git\\usr\\bin;
C:\\Users\\iwasf\\PycharmProjects\\PyBedTime\\lib\\ffmpeg\\bin;
C:\\GLUT;
C:\\GLUT\\include;
C:\\Users\\iwasf\\AppData\\Local\\Microsoft\\WindowsApps;

But hey, In the essence of eliminating the middleman I went ahead and tried calling the full path to executable (i.e C:\Windows\System32\displayswitch.exe) with the same exact results (I also tried case sensitive i.e DisplaySwitch.exe in case that mattered, it didn't).

I have never had a problem like this in 3 years and i'm really at a loss with what to even try. I even tried rebooting my pc which worked just as much as expected.

Other information about my environment and what may have caused this for some reason: Using PyCharm 2018.2.2 IDE, which creates and uses virtualenv for projects.

For the side project I've been working on when this problem first arose, I was originally using python 3.6.6 with flask, pigpio and pyaudio (running this on a raspberry pi) but switched everything over to python 2.7 when I hit a roadblock with mod_wsgi and installing it for python 3.6 which wasn't having any of it.

In PyCharm i've configured it to use both a local interpreter and a remote interpreter, all of which worked flawlessly. The issue with os.system occurred sometime after I made the switch from 3.6 back to 2.7 but I can't directly vouch for that being the exact root cause or not. I made a new virtualenv using 2.7 and deleted the old one using 3.6 from my project.

I've done some intermediate level googling on this issue but 90% of the results were people having the command typed wrong or they were instructed to use subprocess to avoid issues with parameter passing, etc. But as i've tried both and the same command works from command prompt I've convinced myself that it can't be my typing skills.

EDIT: Just had the thought to try it with python 3.6 again, which works fine and i'm even more baffled. If it were a problem specific to 2.7 I would have thought reinstalling would have fixed it.

TheKewlStore
  • 304
  • 1
  • 6
  • can you type `where displayswitch` in the CMD? and try with the full path from python (and show us that it doesn't work with the full path). – Jean-François Fabre Oct 03 '18 at 15:03
  • also the fact that the path is long and has repeats can cause this. Can you simplify your path (there are duplicates) – Jean-François Fabre Oct 03 '18 at 15:04
  • 1
    Probably redirection - that exe is likely 64 bit but your python is running as 32 bit so the path is redirected to \syswow64 – Alex K. Oct 03 '18 at 15:08
  • Okay, so i simplified the path and made up a thorough test case using the full path and where as mentioned. Here are screenshots of the output, as well as a pastebin with it all. https://gyazo.com/7277326c8df76960cbe544af3817e63f https://gyazo.com/e240e24af0b19ce4a114d71768906fd5 https://pastebin.com/FFRN7uLx – TheKewlStore Oct 03 '18 at 15:17
  • If it works with 3.6 then 3.6 is going to be 64 bit unlike the 32 bit 2.7 so the aforementioned redirection does not occur. See https://mail.python.org/pipermail/python-win32/2012-March/012121.html – Alex K. Oct 03 '18 at 15:23
  • Yep this was definitely the problem. Installed the 64-bit version of 2.7 and it works fine. Thanks for the help! – TheKewlStore Oct 03 '18 at 15:26

1 Answers1

1

The problem was 32-bit python versus 64-bit, and I think I know what broke it because when I reinstalled I had both 32-bit and 64-bit python installed. I uninstalled the 32-bit version os 2.7.15 and installed the 64-bit and it's working now. Thanks to Alex K for pointing out the redirection and sorry for the silly question!

TheKewlStore
  • 304
  • 1
  • 6