OSError: [WinError 193] %1 is not a valid Win32 application

I am trying to call a python file “hello.py” from within the python interpreter with subprocess. But I am unable to resolve this error. [Python 3.4.1].

import subprocess    
subprocess.call(['hello.py', 'htmlfilename.htm'])
Traceback (most recent call last):
  File "<pyshell#42>", line 1, in <module>
    subprocess.call(['hello.py', 'htmlfilename.htm'])
  File "C:Python34libsubprocess.py", line 537, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:Python34libsubprocess.py", line 858, in __init__
    restore_signals, start_new_session)
  File "C:Python34libsubprocess.py", line 1111, in _execute_child
OSError: [WinError 193] %1 is not a valid Win32 application

Also is there any alternate way to “call a python script with arguments” other than using subprocess? Thanks in advance.

11 Answers

The error is pretty clear. The file hello.py is not an executable file. You need to specify the executable:

subprocess.call(['python.exe', 'hello.py', 'htmlfilename.htm'])

You’ll need python.exe to be visible on the search path, or you could pass the full path to the executable file that is running the calling script:

import sys
subprocess.call([sys.executable, 'hello.py', 'htmlfilename.htm'])

Python installers usually register .py files with the system. If you run the shell explicitly, it works:

import subprocess
subprocess.call(['hello.py', 'htmlfilename.htm'], shell=True)
# --- or ----
subprocess.call('hello.py htmlfilename.htm', shell=True)

You can check your file associations on the command line with

C:>assoc .py

C:>ftype Python.File
Python.File="C:Python27python.exe" "%1" %*

I got the same error while I forgot to use shell=True in the subprocess.call.

subprocess.call('python modify_depth_images.py', shell=True)

Running External Command

To run an external command without interacting with it, such as one would do with os.system(), Use the call() function.

import subprocess

Simple command subprocess.call([‘ls’, ‘-1’], shell=True)

All above solution are logical and I think covers the root cause, but for me, none of the above worked. Hence putting it here as may be helpful for others.

My environment was messed up. As you can see from the traceback, there are two python environments involved here:

  1. C:UsersexampleAppDataRoamingPythonPython37
  2. C:UsersexampleAnaconda3

I cleaned up the path and just deleted all the files from C:UsersexampleAppDataRoamingPythonPython37.

Then it worked like the charm.

I hope others may find this helpful.

This link helped me to found the solution.

For me issue got resolved after following steps :

  1. Installing python 32 bit version on windows.
  2. Add newly installed python and it’s script folder(where pip resides in environment variable)

Issue comes when any application you want to run needs python 32 bit variants and you have 64 bit variant

Note : Once you install python 32 bit variant,dont forget to install all required packages using pip of this new python 32 bit variant

OSError: [WinError 193] %1 is not a valid Win32 application

This error is most probably due to this line import subprocess

I had the same issue and had solved it by uninstalling and reinstalling python and anaconda then i used jupyter and wrote pip install numpy this gave me the whole path where it was getting my site-packages from i deleted my site-packages folder and then the error dissappeared. Actually because i had 2 folders for site-packages one with anaconda and other somewhere in app data(which had some issues in it), since i deleted that site-package folder then it automatically started taking my libraries from site-package folder which was with anaconda hence the problem was solved.

Uninstalling numpy from command line / terminal through pip fixed the error for me:

pip uninstall numpy

I solved this by Following steps:

  • 1 > Uninstalled python
  • 2 > removed Python37 Folder from C/program files/ and user/sukhendra/AppData
  • 3 > removed all python37 paths

then only Anaconda is remaining in my PC so opened Anaconda and then it’s all working fine for me

For anyone experiencing this on windows after an update

What happened was that Windows Defender made some changes. Possibly cause running data extraction scripts, but python.exe got reduced to 0kb for that project. Copying the python.exe from another project and replacing it solved for now.

I too faced same issue and following steps in resolution of this.

  1. I removed unnecessary python path from system except anaconda path.
  2. C:Users<user-Name>AppDataRoaming<python> = remove any unnecessary python files /folder. these files may interfere with current execution.

Regards Vj

The file hello.py is not an executable file. You need to specify a file like python.exe

try following:

import sys
subprocess.call([sys.executable, 'hello.py', 'htmlfilename.htm'])

Leave a Reply

Your email address will not be published. Required fields are marked *