首页 > 解决方案 > 从 git 推送和拉取后,我的 python exe (cx_freeze) 不起作用

问题描述

问题在于运行我的代码。在项目中,WPF (C#) 应用程序应该运行使用 cx_freeze 制作的可执行 python 文件。问题是它仅在我将项目作为 .zip 传递给其他人时才有效。当我尝试推送到 git 然后拉到某个地方时,此代码 cmd 会引发错误,如下所示。我看到有相同错误的线程,其他人说应该取消设置 PYTHONHOME nad PYTHONPATH。但正如您所看到的,它们没有设置(默认设置)。项目包含登录后在后台运行 python .exe 代码的 WPF 应用程序。带有构建代码的文件夹包含在 WPF 中运行的 main.exe 文件、带有 python 包(.py 文件)的文件夹、带有文件的文件夹 lib,您可以在屏幕和文件 - python3.dll、python38.dll。我想让它不仅从 .zip 包中工作,而且在从 git 存储库中提取之后。

使用 cx_freeze 添加的文件夹

Python path configuration:
  PYTHONHOME = (not set)
  PYTHONPATH = (not set)
  program name = 'C:\Users\d4wt0\Desktop\pracka\wpf1207\desktopapp2\experiment_stream\build\exe.win-amd64-3.8\main.exe'
  isolated = 0
  environment = 0
  user site = 1
  import site = 0
  sys._base_executable = 'C:\\Users\\d4wt0\\Desktop\\pracka\\wpf1207\\desktopapp2\\experiment_stream\\build\\exe.win-amd64-3.8\\main.exe'
  sys.base_prefix = ''
  sys.base_exec_prefix = ''
  sys.executable = 'C:\\Users\\d4wt0\\Desktop\\pracka\\wpf1207\\desktopapp2\\experiment_stream\\build\\exe.win-amd64-3.8\\main.exe'
  sys.prefix = ''
  sys.exec_prefix = ''
  sys.path = [
    'C:\\Users\\d4wt0\\Desktop\\pracka\\wpf1207\\desktopapp2\\experiment_stream\\build\\exe.win-amd64-3.8\\lib\\library.zip',
    'C:\\Users\\d4wt0\\Desktop\\pracka\\wpf1207\\desktopapp2\\experiment_stream\\build\\exe.win-amd64-3.8\\lib',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00003040 (most recent call first):
<no Python frame>

从 c# 运行 exe:

static readonly string path_prefix = "\\..\\..\\..\\..";
static string dir_path = Directory.GetCurrentDirectory();

private static void run_file(string fileName, bool wait = false)
        {
            ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
            info.Arguments = "/K " + fileName;
            Process.Start(info);
        }

public static void select_ROI()
        {
            if (run_py)
            {
                string select_roi = dir_path + path_prefix + "\\experiment_stream\\build\\exe.win-amd64-3.8\\main.exe";
                run_file(select_roi);
            }
        }

setup.py 使用 cx_freeze 构建 exe:

import sys
from cx_Freeze import setup, Executable

includefiles = ['calibration_package/points.csv', 'ffmpeg/', 'calibration_package/',
                'communication_package/', 'gaze_tracker_package/', 'model_package/', 'recorder_package/',
                'log_package/', 'CASES/', 'configs/'
                ]

packages = ['os', 'socket', 'cv2', 'pathlib', 'glob', 'time', 'numpy', 'pandas',
            'subprocess', 'signal', 'sklearn', 'json', 'scipy', 'random',
            'imutils.video', 'PIL', 'threading', 'queue', 'configparser'
            ]

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

setup(
    name = "ET",
    version = "0.2",
    description = "eye_tracker",
    options = {"build_exe": {'packages': packages, 'include_files': includefiles}},
    executables = [Executable("main.py")]
)

标签: pythonwpfcx-freezepythonpath

解决方案


推荐阅读