Python OSError: [WinError 193] %1 不是有效的 Win32 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25651990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:26:20  来源:igfitidea点击:

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

pythonsubprocesspython-3.4

提问by Caxton

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].

我正在尝试从带有子进程的 python 解释器中调用 python 文件“hello.py”。但我无法解决此错误。[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:\Python34\lib\subprocess.py", line 537, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Python34\lib\subprocess.py", line 858, in __init__
    restore_signals, start_new_session)
  File "C:\Python34\lib\subprocess.py", line 1111, in _execute_child
    startupinfo)
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.

除了使用子进程之外,还有其他方法可以“使用参数调用python脚本”吗?提前致谢。

采纳答案by David Heffernan

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

错误很明显。该文件hello.py不是可执行文件。您需要指定可执行文件:

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

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

您需要python.exe在搜索路径上可见,或者您可以将完整路径传递给运行调用脚本的可执行文件:

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

回答by tdelaney

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

Python 安装程序通常向系统注册 .py 文件。如果您显式运行 shell,它会起作用:

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
.py=Python.File

C:\>ftype Python.File
Python.File="C:\Python27\python.exe" "%1" %*

回答by Mona Jalal

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

当我忘记shell=Truesubprocess.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)

运行外部命令

要在不与其交互的情况下运行外部命令,例如使用 os.system(),请使用 call() 函数。

导入子流程

简单命令 subprocess.call(['ls', '-1'], shell=True)

回答by Muhammad Saad Najib

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

OSError: [WinError 193] %1 不是有效的 Win32 应用程序

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 numpythis 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.

我遇到了同样的问题,并通过卸载并重新安装 python 和 anaconda 解决了它,然后我使用了 jupyter 并编写了pip install numpy这给了我从我删除站点包文件夹中获取站点包的完整路径,然后错误消失了。实际上,因为我有 2 个站点包文件夹,一个是 anaconda,另一个是应用程序数据中的某个位置(其中存在一些问题),因为我删除了该站点包文件夹,然后它会自动开始从站点包文件夹中获取我的库有了anaconda,问题就解决了。

回答by SaimumIslam27

Uninstalling numpyfrom command line / terminal through pipfixed the error for me:

numpy通过命令行/终端卸载pip为我修复了错误:

pip uninstall numpy

pip uninstall numpy

回答by Red Boy

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 environmentwas messed up. As you can see from the traceback, there are two python environments involved here:

我的environment被搞砸了。从回溯中可以看出,这里涉及到两个 python 环境:

  1. C:\Users\example\AppData\Roaming\Python\Python37
  2. C:\Users\example\Anaconda3
  1. C:\Users\example\AppData\Roaming\Python\Python37
  2. C:\Users\example\Anaconda3

I cleaned up the path and just deleted all the files from C:\Users\example\AppData\Roaming\Python\Python37.

我清理了路径并从C:\Users\example\AppData\Roaming\Python\Python37.

Then it worked like the charm.

然后它就像魅力一样工作。

I hope others may find this helpful.

我希望其他人可能会觉得这很有帮助。

This linkhelped me to found the solution.

链接帮助我找到了解决方案。