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

63,741

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.

Share:
63,741
Ashish Jain
Author by

Ashish Jain

Web Application Development in general, cooked web applications with few more technologies as Python,HTML,JavaScript. C,Python(Django,Flask,Bottle,PyGTK),JavaScript,JQuery,HTML,MySQL database, primarily. Interaction with android & iOS application, serial connection through python. Worked on developments from scratch, requiring decision making on what to use and what not to. My Stackoverflow Career Profile

Updated on November 28, 2020

Comments

  • Ashish Jain
    Ashish Jain over 3 years

    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

  • Ashish Jain
    Ashish Jain over 10 years
    After using your above code, same login file is calling again and again. i am not able to go to start.py means 2nd window.
  • Ashish Jain
    Ashish Jain over 10 years
    I agreed with ur opinion, on another system it is searching %PATH% but it is not available in another system. and 2nd i included all script file in setup file,so all files are coming with exe directory. Now problem is how to resolve becoz same error is coming after used ur code.
  • Ashish Jain
    Ashish Jain over 10 years
    Thanks abarnert for ur comment. I have resolved this issue, actually it is occurring bcoz of other system doesn't understand python bcoz python is not installed in that system. so i replace above line with this line subprocess.call("start.py", shell=True)
  • abarnert
    abarnert over 10 years
    @AshishJain: That's not a very good solution. The whole point of using py2exe is to be able to run on systems that don't have Python (and any third-party modules you require) installed. If you need that, your solution won't work; if you don't need that, you shouldn't be adding the complexity of py2exe in the first place; python setup.py bdist --formats=wininst should be all you need.