1

I am facing a weird issue with python.exe in Windows. When I enter python in CMD, the interactive Python shell opens in a new console window.

When I delete python.exe and try to repair Python using the installer, it works fine and opens the interactive shell in the same console window. But when I refresh the "C:\python27" folder, or install any new module, the python.exe icon changes and the issue returns.

I found a few answers in the following link but nothing worked out: Python.exe opens in a new console window.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
  • When the issue occurs, try running `where python` from the command window and share the results as part of your question. – Grismar Jan 06 '20 at 04:10
  • What is the value of `sys.executable` in Python? Also, in Task Manager, what's that path of python.exe in the details tab? – Eryk Sun Jan 06 '20 at 05:20
  • @Grismar When i run **where python** from command line, it was printing below path _'C:\Python27\python.exe'_ – Lokesh kumar Jan 08 '20 at 04:23
  • @ErykSun When i run **sys.executable** in python console, it was printing below path _'c:\\python27\\python.exe\xa0'_ – Lokesh kumar Jan 08 '20 at 04:24
  • Check the following: run `import os` and then `[n for n in os.listdir(u'C:/Python27') if n.startswith(u'python.exe')]`. The `u` prefix is for Unicode strings in 2.7, because I want the native Unicode result from Windows, without passing through any implicit encoding/decoding stages. – Eryk Sun Jan 08 '20 at 05:45
  • @ErykSun As suggested, i exectued given lines in python shell. Got a list of 2 items. **[u'python.exe', u'python.exe\xa0']** – Lokesh kumar Jan 08 '20 at 05:53
  • Something is creating that extra file with a trailing non-breaking space (i.e. "\xa0"). It's an invisible character, so it won't be seen in most cases. Also, code that strips trailing whitespace when matching "python.exe", as is common in Windows, may end up running that file instead of the real "python.exe". Taken together, I'm inclined to think this is malware. Check whether the same exists for "pythonw.exe": `[n for n in os.listdir(u'C:/Python27') if n.startswith(u'pythonw.exe')]`. – Eryk Sun Jan 08 '20 at 06:58
  • @ErykSun as you expected, 2 files exists with name pythonw ['pythonw.exe', 'pythonw.exe\xa0'] – Lokesh kumar Jan 08 '20 at 07:03
  • Uninstall Python 2.7 and then open the command prompt and wipe that directory completely: `rd /q /s C:\Python27`. Then try reinstalling. If the bad files come back, your machine is definitely infected with malware. – Eryk Sun Jan 08 '20 at 07:06
  • @ErykSun Bad files are coming back after the re-installation Eryk. I tried re-installing the windows-7 2 days ago, but this issue repeats. – Lokesh kumar Jan 08 '20 at 07:12
  • Uninstalling and reinstalling doesn't necessarily remove the original installation directory and any bad files it might contain. That's why I inserted the extra step of deleting the directory manually before reinstalling. – Eryk Sun Jan 08 '20 at 07:24
  • @ErykSun Ya. i deleted that folder using the cmd only. As you said it was a malware, I installed MalwareBytes software and scanned the python folder. Your guess was right. It had shown those python files as malware and deleted them. And now after fresh installation of python, everything was working fine. Thank you for your suggestion. I will scan up all the drives and delete any infected files. Thank you. – Lokesh kumar Jan 08 '20 at 08:18

0 Answers0