My python project has both a main.py
and key.json
. When I use pyinstaller to download main.py
as a single executable, the console flashes and closes immediately. How do I fix this? Is anyone else having the same problem?
Here is 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)
Note: main.py
works and the console shows up. I think it might have something to do when main.py converts to an executable. It may be due to the startup not being able to function due to key.json
not being compiled in the executable.
You'll need an 'input' function to wait for the user to input the password.
pwd = input("Enter New Password: ")
And then use the value in the pwd variable to encrypt.
Update:
One way to debug exe from pyinstaller would be to execute it from command line. You can try that. Your latest code works when I have a key.json in the same folder as the exe. As you are writing keys into key.json, you'll need to have the file there.
From command line:
Directy Executing Exe: