“WindowsError:[错误2]系统找不到指定的文件”没有解决 [英] "WindowsError: [Error 2] The system cannot find the file specified" is not resolving

查看:6938
本文介绍了“WindowsError:[错误2]系统找不到指定的文件”没有解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过py2exe创建了具有多个文件的python项目的exe文件。
当我在我的系统中运行这个exe文件。它工作正常,但如果我把它放在另一个系统,然后它打开登录窗体,然后它不会去下一个窗口,我写在第二个Python文件。它会给我以下错误日志文件。

 追踪(最近一次调用最后):
文件login.py 线246,在DataReader的
档 subprocess.pyo,线路711,在__init__
档 subprocess.pyo,线路948,在_execute_child
WindowsError:[错误2]系统找不到指定的文件

我知道这是重复的问题,但我已经尝试了很多解决方案但我没有解决这个问题。有人帮我解决了这个问题。



并且在登录成功之后,它将通过此​​代码转到start.py文件,但它不会发生并且出现上述错误。 / p>

  subprocess.call([python,./start.py])
这里至少有两个问题。




首先,您不能只使用 python 作为可执行文件。



在你的系统上,<$ c c>上有 python $ c>%PATH%,它是正确的Python版本,包含你依赖的所有模块等等。但是你不能依赖所有的模块用户。如果可以的话,你不会在第一时间打扰 py2exe



显然,在另一个你正在测试的机器上,%PATH%上根本没有任何名为 python 的东西,所以你得到一个Windows Error 2。



无论如何,您希望使用您的脚本使用的相同的 Python。






与此同时,没有理由期望 start.py 位于当前工作目录中。它(希望)与父脚本位于相同的目录中,但可能不会成为工作目录。通常情况下,Windows程序的启动类似C:\或WINNT目录或用户的主目录,并且版本与版本不同。



当然,在开发时,通常使用命令提示符,脚本的目录作为您的工作目录,无论何时运行脚本,或者您使用的IDE都等效。所以它恰好工作。但是,当从.exe运行时,你不能指望它。



(这个调试将更加有趣,子进程将成功启动并立即完成,但没有做任何可见的事情,你的父脚本不会有任何错误,因为你没有检查退出代码或stderr,这会使调试变得很有趣,你应该使用 check_call ,而不是 call 。)



无论如何,如果你想要脚本要找到与它自己在同一目录中的另一个脚本,则需要明确说出。






因此,要解决这两个问题这些问题:

 进口OS 
进口SYS
mypath中= os.path.abspath则(__ FILE__)
MYDIR = os.path.dirname(my_path的)
开始= os.path.join(MYDIR start.py)
subprocess.call([sys.executable,开始])






最后一件事:米甚至不知道你再实际上捆绑 start.py 到您的分发包的。在你的机器上,它的工作地点显然是 C:\Python27\start.py 。但是在你正在测试的机器上...它存在于任何地方吗?

py2exe 这样的工具可以自动找到依赖关系,即 import ,但是如果你只是通过 subprocess 在不同的解释器实例中运行一个脚本,你将不得不告诉它(在你的 setup.py 中)包含该脚本。


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.

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.

And After login successfully it will go to start.py file by this code, But it is not going and giving above error.

    subprocess.call(["python", "./start.py"])

Thanks in advance

解决方案

There are at least two problems here.


First, you can't just use python as the executable.

On your system, you've got python on the %PATH%, and it's the right Python 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.

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.

At any rate, you want to run with the same Python that your script is using.


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.

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.

(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.)

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.

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.

这篇关于“WindowsError:[错误2]系统找不到指定的文件”没有解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆