“WindowsError: [Error 2] The system cannot find the file specified” is not resolving

最后都变了- 提交于 2019-12-05 07:10:34

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!