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

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

There are at least two problems here.


First, you cant just use python as the executable.

On your system, youve got python on the %PATH%, and its the right Python version, with all the modules you depend on, etc. But you cant rely on that for all your users. If you could, you wouldnt bother with py2exe in the first place.

And obviously, on the other machine youre testing on, theres 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, theres no reason to expect start.py to be in the current working directory. Its (hopefully) in the same directory as the parent script, but that wont be the working directory. Typically, a Windows program starts up with something like C: or the WINNT directory or the users home directory, and its different from version to version.

Of course, during development, youre using the command prompt, with the scripts directory as your working directory whenever you run the script, or youre using an IDE that effectively does the equivalent. So it happens to work. But when run from the .exe, you cant 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 youre 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 thats 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, Im not even sure youre actually bundling start.py into your distributable package at all. On your machine, where it works, its apparently in C:Python27start.py. But on the machine youre testing on… does it exist anywhere? If not, you obviously cant run it.

Tools like py2exe can automatically find dependencies that you import, but if youre just running a script in a different interpreter instance via subprocess, youre going to have to tell it (in your setup.py) to include that script.

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

Leave a Reply

Your email address will not be published. Required fields are marked *