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

Ashish Jain picture Ashish Jain · Sep 12, 2013 · Viewed 54.7k times · Source

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

Answer

abarnert picture abarnert · Sep 12, 2013

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.