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

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

Leave a Comment