Python “WindowsError: [错误 2] 系统找不到指定的文件”未解决
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18757127/
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
"WindowsError: [Error 2] The system cannot find the file specified" is not resolving
提问by Ashish Jain
I have created exe file of my python project by py2exe which have number of files. when i run this exe file in my system. it works fine but if i put it in another system then it opens login form, then after it doesn't go to next window which i have written in 2nd python file. it gives me below error in log file.
我已经通过 py2exe 创建了我的 python 项目的 exe 文件,其中有很多文件。当我在我的系统中运行这个 exe 文件时。它工作正常,但如果我将它放在另一个系统中,那么它会打开登录表单,然后它不会转到我在第二个 python 文件中编写的下一个窗口。它在日志文件中给了我以下错误。
Traceback (most recent call last):
File "login.py", line 246, in DataReader
File "subprocess.pyo", line 711, in __init__
File "subprocess.pyo", line 948, in _execute_child
WindowsError: [Error 2] The system cannot find the file specified
I know that it is duplicate question but I have tried many solution of stackoverflow but i didn't resolve this issue. Somebody help me for resolving this issue.
我知道这是重复的问题,但我已经尝试了很多 stackoverflow 的解决方案,但我没有解决这个问题。有人帮我解决这个问题。
And After login successfully it will go to start.py file by this code, But it is not going and giving above error.
登录成功后,它将通过此代码转到 start.py 文件,但它不会并给出上述错误。
subprocess.call(["python", "./start.py"])
Thanks in advance
提前致谢
采纳答案by abarnert
There are at least two problems here.
这里至少有两个问题。
First, you can't just use python
as the executable.
首先,您不能仅用python
作可执行文件。
On your system, you've got python
on the %PATH%
, and it's the rightPython version, with all the modules you depend on, etc. But you can't rely on that for all of your users. If you could, you wouldn't bother with py2exe
in the first place.
在您的系统上,您已经python
安装了%PATH%
,并且它是正确的Python 版本,以及您依赖的所有模块等。但是您不能对所有用户都依赖它。如果可以,你一开始就不会打扰py2exe
。
And obviously, on the other machine you're testing on, there's just nothing at all named python
on the %PATH%
, so you get a WindowsError 2.
很显然,你正在测试另一台机器上,有一个在所有命名只会什么都得不到python
的%PATH%
,所以你得到一个WindowsError 2。
At any rate, you want to run with the samePython that your script is using.
无论如何,您希望使用您的脚本正在使用的相同Python运行。
Meanwhile, there's no reason to expect start.py
to be in the current working directory. It's (hopefully) in the same directory as the parent script, but that probably won't be the working directory. Typically, a Windows program starts up with something like C:\ or the WINNT directory or the user's home directory, and it's different from version to version.
同时,没有理由期望start.py
在当前工作目录中。它(希望)与父脚本位于同一目录中,但这可能不是工作目录。通常,Windows 程序以 C:\ 或 WINNT 目录或用户的主目录之类的内容启动,并且因版本而异。
Of course during development, you're generally using the command prompt, with the script's directory as your working directory whenever you run the script, or you're using an IDE that effectively does the equivalent. So it happens to work. But when run from the .exe, you can't count on that.
当然,在开发过程中,您通常使用命令提示符,无论何时运行脚本,都将脚本目录作为工作目录,或者您正在使用有效执行等效操作的 IDE。所以它碰巧工作。但是当从 .exe 运行时,你不能指望它。
(This one will be even more fun to debug. The subprocess will start successfully and immediately finish, without doing anything visible. Your parent script will have no idea that anything went wrong, because you're not checking the exit code or the stderr, which will make things lots of fun to debug. You really should be using check_call
, not call
.)
(这个调试起来会更有趣。子进程将成功启动并立即完成,不会做任何可见的事情。您的父脚本将不知道出现了什么问题,因为您没有检查退出代码或标准错误,这将使调试变得很有趣。您确实应该使用check_call
,而不是call
。)
Anyway, if you want your script to find another script that's in the same directory as itself, you need to say so explicitly.
不管怎样,如果你想让你的脚本找到另一个和它在同一个目录下的脚本,你需要明确的说出来。
So, to fix both of those problems:
因此,要解决这两个问题:
import os
import sys
mypath = os.path.abspath(__file__)
mydir = os.path.dirname(my_path)
start = os.path.join(mydir, "start.py")
subprocess.call([sys.executable, start])
One last thing: From your comments, I'm not even sure you're actually bundling start.py
into your distributable package at all. On your machine, where it works, it's apparently in C:\Python27\start.py
. But on the machine you're testing on…?does it exist anywhere? If not, you obviously can't run it.
最后一件事:根据您的评论,我什至不确定您实际上是否真的捆绑start.py
到您的可分发包中。在你的机器上,它工作的地方,它显然在C:\Python27\start.py
. 但是在您正在测试的机器上……它存在于任何地方吗?如果没有,您显然无法运行它。
Tools like py2exe
can automatically find dependencies that you import
, but if you're just running a script in a different interpreter instance via subprocess
, you're going to have to tell it (in your setup.py
) to include that script.
像这样的工具py2exe
可以自动找到您的依赖项import
,但是如果您只是通过在不同的解释器实例中运行脚本subprocess
,您将不得不告诉它(在您的 中setup.py
)包含该脚本。