将.py转换为.exe,出现错误OSError:[WinError 6]句柄无效

问题描述 投票:0回答:1

我有一个在 VS Code 中运行良好的 python 程序。我正在第一次尝试将其转换为 exe。这是一个基本程序,用户可以从 9 份晚餐的列表中选择多份晚餐,然后生成一个购物清单。成分位于 csv 文件中。 当我尝试运行 exe 时,出现此错误

Traceback (most recent call last):
  File "main.py", line 52, in <module>
OSError: [WinError 6] The handle is invalid

谁能告诉我问题是什么?

我尝试询问 chatGPT 几次,但它建议的所有内容都给了我错误,老实说,我做了很多更改,我不再知道自己在做什么。这是可以挽救的还是我应该放弃它并重新开始?

chatGPT 的建议和相应错误示例:
“您可以使用 PyInstaller.utils.cli 模块中的输入函数而不是内置输入函数。”
当我说这给了我错误“无法解析导入“PyInstaller.utils.cli”PylancereportMissingImports(module) PyInstaller”
它说“使用 PyInstaller.utils.inputhook 模块以与 PyInstaller 编译的可执行文件兼容的方式处理输入”
但这也给出了一个错误,所以它说
“处理 PyInstaller 编译的可执行文件中的输入。我们可以使用输入函数和 sys.stdin 重定向来确保输入正确工作。”
这给了我错误“”Traceback(最近一次调用最后一次):文件“main.py”,第 52 行,in 这也产生了一个错误,所以它回复了
“我们可以使用 PyInstaller 钩子来修补 sys.stdin 流”,这就是我现在所处的位置,但仍然一无所知。

这是我当前的代码:

import csv
import sys

def read_ingredients(file_name):
    ingredients = []
    with open(file_name, newline='') as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            ingredients.append(row[0])
    return ingredients

def generate_shopping_list(selected_dinners):
    all_ingredients = []
    for dinner in selected_dinners:
        file_name = dinner.lower().replace(" ", "") + ".csv"
        ingredients = read_ingredients(file_name)
        all_ingredients.extend(ingredients)
    unique_ingredients = list(set(all_ingredients))
    return unique_ingredients

def main():
    dinners = [
        "Spaghetti Bolognese",
        "Nasi Goreng",
        "Honey and Mustard Chicken Thighs",
        "Honey and Garlic Chicken Thighs",
        "Chocolate and Peanut Butter Overnight Oats",
        "Chickpea and Veg Soup",
        "Carbonara",
        "Beef Broccoli and Rice",
        "Banana Bread"
    ]

    print("Please pick which dinners you will have this week:")
    for index, dinner in enumerate(dinners, start=1):
        print(f"{index}. {dinner}")

    selected_indices = input("Enter the numbers corresponding to your selected dinners separated by spaces: ").split()
    selected_dinners = [dinners[int(index) - 1] for index in selected_indices]

    input("Press Enter to generate the shopping list...")

    shopping_list = generate_shopping_list(selected_dinners)

    print("\nShopping List:")
    for ingredient in shopping_list:
        print(ingredient)

if __name__ == "__main__":
    if getattr(sys, 'frozen', False):
        # If the program is running as a bundled executable
        sys.stdin = open(0)
    main()
python windows executable
1个回答
0
投票

在 python 3.8 的文件中添加 stdin=subprocess.PIPE:C:\Program Files (x86)\Python38-32\Libos.py 第 983 行到 991 行

proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT,bufsize=缓冲)

© www.soinside.com 2019 - 2024. All rights reserved.