Python 可执行控制台未显示

Python executable console not showing up

我的 python 项目有 main.pykey.json。当我使用 pyinstaller 将 main.py 作为单个可执行文件下载时,控制台会闪烁并立即关闭。我该如何解决?还有其他人遇到同样的问题吗?

这里是main.py:

import random
import json
import os
file_path = 'key.json'
char = 0
letters = list('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%&*?+-')
#key = assign randomized 2-3 characters to each letter
print('Want a new encryption key? Type "New_Key"')
#startup check if key.json is empty
if os.stat(file_path).st_size == 0:
    key = []
    while char < len(letters):
        rand = random.randint(0, len(letters) - 1)
        key.append(letters[rand])
        char = char + 1
    char = 0
    while char < len(letters):
        rand = random.randint(0, len(letters) - 1)
        key[char] = key[char] + letters[rand]
        char = char + 1
    char = 0
    while char < len(letters):
        rand = random.randint(1, 2)
        if rand == 2:
            rand = random.randint(0, len(letters) - 1)
            key[char] = key[char] + letters[rand]
            if char > 0:
                key[char] = " " + key[char]
            char = char + 1
        else:
            if char > 0:
                key[char] = " " + key[char]
            char = char + 1
    key = "".join(key)
    jsonFile = open("key.json", "w")
    jsonFile.write(json.dumps(key))
    jsonFile.close()
    # convert key.json contents to usable list
    with open("key.json") as f:
        key = [key.rstrip() for key in f]
    key = ''.join(map(str, key)).replace('"', "")
    key = key.split()
#Input password for encryption
#The input “New_Key” creates a new encryption key in key.json
repeat = 0
while repeat < 1:
    inp = input('Enter: ')
    if inp == "New_Key":
        char = 0
        open("key.json", "w").close()
        key = []
        while char < len(letters):
            rand = random.randint(0, len(letters) - 1)
            key.append(letters[rand])
            char = char + 1
        char = 0
        while char < len(letters):
            rand = random.randint(0, len(letters) - 1)
            key[char] = key[char] + letters[rand]
            char = char + 1
        char = 0
        while char < len(letters):
            rand = random.randint(1, 2)
            if rand == 2:
                rand = random.randint(0, len(letters) - 1)
                key[char] = key[char] + letters[rand]
                if char > 0:
                    key[char] = " " + key[char]
                char = char + 1
            else:
                if char > 0:
                    key[char] = " " + key[char]
                char = char + 1
        key = "".join(key)
        jsonFile = open("key.json", "w")
        jsonFile.write(json.dumps(key))
        jsonFile.close()
        # convert key.json contents to usable list
        with open("key.json") as f:
            key = [key.rstrip() for key in f]
        key = ''.join(map(str, key)).replace('"', "")
        key = key.split()
    else:
        inp_num = 0
        output = []
        # convert key.json contents to usable list
        with open("key.json") as f:
            key = [key.rstrip() for key in f]
        key = ''.join(map(str, key)).replace('"', "")
        key = key.split()
        while inp_num < len(inp):
            list_num = 0
            while list_num < len(letters):
                if inp[inp_num] == letters[list_num]:
                    output.append(key[list_num])
                    list_num = list_num + 1
                else:
                    list_num = list_num + 1
            inp_num = inp_num + 1
        output = "".join(output)
        print(output)

注意:main.py 有效并且控制台出现。我认为它可能与 main.py 转换为可执行文件有关。可能是由于 key.json 没有被编译成可执行文件导致启动无法运行。

您需要一个 'input' 函数来等待用户输入密码。

pwd = input("Enter New Password: ")

然后使用pwd变量中的值进行加密

更新:

从 pyinstaller 调试 exe 的一种方法是从命令行执行它。你可以试试看。当我在与 exe 相同的文件夹中有一个 key.json 时,您的最新代码有效。当您将密钥写入 key.json 时,您需要将文件放在那里。

从命令行:

直接执行 Exe: