cx_freeze ImportError when executing file

流过昼夜 提交于 2019-12-07 19:39:10

问题


The program is designed to capture the title bar of the users foreground window and append that title along with the datetime to a log file.

When I run this code on my computer it works, however when I run the executable on another computer I get the error you can see below:

Python file:

from win32gui import GetForegroundWindow, GetWindowText
from datetime import datetime
from time import sleep
from os.path import join

log_path = r'C:\Office Viewer\OV_Log.txt'

while True:

    window_name = GetWindowText(GetForegroundWindow())

    current_time = datetime.strftime(datetime.now(), '%Y/%m/%d_%H:%M:%S, ')

    txt = join('\n',current_time, window_name)

    with open(log_path, 'a') as log_file:
        log_file.write(txt)

    sleep(5)

setup.py:

import sys
from cx_Freeze import setup, Executable

base = None
if (sys.platform == "win32"):
    base = "Win32GUI"

#build_exe_options = {'include_files': ['re']}
build_exe_options = {"packages": ["re"]}

setup(  name = "Foreground Window Montior",
        version = "0.1",
        description = "Query the foreground window.",
        options = {'build_exe': build_exe_options},
        executables = [Executable("actWin_Query.py", base=base)])

Error is:

Can anyone recommend what I need to do to solve this issue please?

The issue appears to be that C:\Python33\ etc... doesn't exist on the other computer. How can I make the program be completely dependent from the directory the .exe is housed in?


回答1:


Ah, the joys of packaging standalone Python apps for Windows! Anybody that tells you this is easy hasn't tried it. But don't give up, the result is usually worth it if you can get past the initial situps.

The paths emitted in your frozen executable's error message are most likely a red herring that you can safely ignore. It looks like your app is (indirectly) pulling in a DLL that isn't automatically included by cx_Freeze, and isn't available on the target machine. Figuring out exactly which DLL is missing is the tricky part.

I personally tried running the setup.py you posted on a Windows 7 machine to generate an EXE and support files for your Python script. I then copied the bundle to a Windows XP box. Unfortunately, it ran just fine for me, so I can only give you generic troubleshooting advice. Here's what I'd try:

  1. Download Dependency Walker to the target machine and open your cx_Freeze-generated actWin_Query.exe binary.
  2. Try to determine which DLL is failing to load
  3. Back on your build machine, use the cx_Freeze include-files option to bring in the missing DLL(s)
  4. Rebuild the app
  5. Try running again on the target

Sorry I can't give more specific advice than this. There are so many possible combinations of Windows/Python, plus 32-/64-bit and C runtime variants that it's hard to pinpoint an exact cause. But I've had similar problems at least a dozen times, and it always seems to boil down to missing files.

The case I run into most often is: I'll build the app on a machine that happens to have, say, Visual C++ 2010 [Express] installed, and then find that it fails to run on a machine that does not have VC++2010 installed. Installing the VC++2010 Redistributable on the target often solves the problem, or bundling its installing into a 'super-installer' that also installs the cx_Freeze-generated files. (I suspect the problem you're seeing is slightly different than this, just mentioning it in case it's helpful.) Good luck!



来源:https://stackoverflow.com/questions/24553439/cx-freeze-importerror-when-executing-file

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