我真的很想让我的Py项目成为一个独立的exe,如果您对如何改进有任何建议,我非常欢迎您

问题描述

  1. 这是我的代码,我真的很想使其成为独立的可执行文件,并且我使用的是python 3.8.3。
  2. 如果您有任何即兴建议,或者设法找到一种方法,我将非常高兴。
  3. 我已经离线尝试了命令CD-C:\ Users \ chris \ OneDrive \ Desktop \ K.A.TAi,然后输入该命令,然后将其放入命令pyinstaller --onefile K.A.T_AI_offline.py
  4. 但它最终无法正常工作,并且确实在dist文件夹中提供了一个文件
  5. 所以请您帮我制作一个独立的可执行文件,以便我可以向我的计算机科学老师和我的朋友们展示,而无需他们下载python和其他模块。
    # All the modules being imported
    import sys
    import time
    from tkinter import Tk,messageBox,simpledialog
    import PySimpleGUI as sg
    import pyttsx3

    # The voice settings
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    engine.setProperty('voice',voices[1].id) #changing index changes voices but ony 0 and 1 are working here

    # This creates a dictionary
    the_master = {}
    the_world = {}
    the_word= {}

    # Defining how to read the info in capital_data.txt
    def read_from_file():
        with open('capital_data.txt') as file:
            for line in file:
                line = line.rstrip('\n')
                country,city = line.split('/')
                the_world[country] = city

    # Defining how to write new info in new_data.txt            
    def write_to_file(country_name,city_name):
        with open('new_data.txt','a') as file:
            file.write('\n' + country_name + '/' + city_name)

    def read_from_master_file():
        with open('master_data.txt') as file:
            for line in file:
                line = line.rstrip('\n')
                MName,MName = line.split('/')
                the_master[MName]= MName
                
    def write_to_master_file(master_name):
        with open('master_data.txt','a') as file:
            file.write('\n' + master_name + '/' + master_name)

    def read_from_alphabet_file():
        with open('phonetic_alphabet.txt') as file:
            for line in file:
                line = line.rstrip('\n')
                letter,word = line.split('/')
                the_word[letter] = word

    # Popup Box to ask wether K.A.T kNow this user 
    sg.theme('DarkBlack')
    layout =[[sg.Text('Are You One Of My Masters')],[sg.ReadFormButton('Yes'),sg.ReadFormButton('No')]]
    form = sg.FlexForm('Do I KNow You')
    form.Layout(layout)
    engine.say('are you already one of my masters')
    engine.runAndWait()

    button,values = form.Read()
    if button is button == 'No':
        form.close()
        print ('Well In That Case')
        print ('Hello user'),engine.say('well,in that case,Hello user'),engine.runAndWait(),print ('I am KAT'),engine.say('I am KAT'),print ('Which stands for KNow.Alot.of.Things'),engine.say('Which stands for,KNow a lot of things'),engine.runAndWait()

    # see's if K.A.T kNow the Master
    if button is button == 'Yes':
        form.close()
        engine.say('oh,so which master are you?')
        engine.runAndWait()
        root = Tk()
        root.withdraw()
        read_from_master_file()
        query_master = simpledialog.askstring('Name Of Master','Which Master Are You?')
        query_master = query_master.capitalize()
        if query_master in the_master:
            master_result = the_master[query_master]
            engine.say('My apologies master,' + master_result)
            engine.runAndWait()
            choice_of_country = ('Would you like to kNow the Capital city of a Country')
            choice_of_phonetic_alphabet = ('Would you like to kNow how to spell a word or letter in the Phonetic Alphabet')    
            yes_master = print (choice_of_country),engine.say('Would you like to kNow the Capital city of a Country.'),print ('OR'),engine.say('or.'),engine.runAndWait,print (choice_of_phonetic_alphabet),engine.say('Would you like to kNow how to spell a word or letter in the Phonetic Alphabet'),engine.say('Which one would you like to kNow,just type 1 or 2'),choice = input('Which one would you like to kNow,just type 1 or 2') 
        else:
            engine.say('sorry you are not one of my masters')
            engine.runAndWait()

    if button is button == 'No':
        form.close()
        engine.say('oh,and,please may you enter the name,that you wish to be called.')
        engine.runAndWait()
        root = Tk()
        root.withdraw()
        new_master = simpledialog.askstring('New Master','Oh And Please May You Enter the name that you wish to be called')
        new_master = new_master.capitalize()
        if new_master == 'Oliver' :
            print('OH well in that case you can just')
            engine.say('OH well in that case you can just')
            print('GET LOST')
            engine.say('GET LOST')
            engine.runAndWait()
            sys.exit(0)
        elif new_master in the_master:
                print('You Are A Master Silly.')
                engine.say('you are already a master,silly,you,are master,' + new_master)
                engine.runAndWait()
                write_to_master_file == False
        elif new_master != 'Oliver':
            write_to_master_file(new_master)
            root.destroy()
            engine.say('hello,'+new_master)
            engine.runAndWait()
            choice_of_country = ('Would you like to kNow the Capital city of a Country')
            choice_of_phonetic_alphabet = ('Would you like to kNow how to spell a word or letter in the Phonetic Alphabet')    
            no_master = print (choice_of_country),just type 1 or 2')

        if simpledialog in (None,'Ok'):
            root.destroy()

        new_master = new_master.capitalize()
        new_master = simpledialog.askstring('New Master','Oh And Please May You Enter the name that you wish to be called')
        the_master[new_master] = new_master

    if choice == '1' :
        capital_cities = ('Well just type in the Country into the little window that will pop up and i\'ll tell you the Capital City')
        print (capital_cities)
        engine.say('Well just type in the Country into the little window that will pop up and i\'ll tell you the Capital City')
        engine.runAndWait()
        # Creates the Tkinter window 
        root = Tk()
        root.withdraw()
        read_from_file()
        engine.say('Now,type the name of the country you would like to kNow the capital city of.')
        engine.runAndWait()
        while True:
            query_country = simpledialog.askstring('Country','Type the name of a country:')
            query_country = query_country.capitalize()

            if query_country in the_world:
                result = the_world[query_country]
                messageBox.showinfo('Answer','The capital city of ' + query_country + ' is ' + result + '!')
                engine.say('The capital city of ' + query_country + ' is ' + result)
                engine.runAndWait()
            else:
                new_city = simpledialog.askstring('Teach me','I don\'t kNow! ' +
                                                'What is the capital city of ' + query_country + '?')
                engine.say('i don\'t kNow!'+
                        'what the capital city of'+query_country+'is'+
                        'but if you kNow you can type it in the Box')
                engine.runAndWait()
                the_world[query_country] = new_city
                write_to_file(query_country,new_city)
                break

    elif choice == '2' :
        phonetic_alphabet = ('Well just type in the Word or Letter into the little window that will pop up and i\'ll tell you how to write it out,but in using the phonetic alphabet')
        print (phonetic_alphabet)
        read_from_alphabet_file()
        engine.say('Well just type in the Word or Letter into the little window that will pop up and i\'ll tell you how to write it out,but in using the phonetic alphabet')
        engine.runAndWait()
        root = Tk()
        root.withdraw()
        while True:
            query_word = simpledialog.askstring('Word','Type a word or letter :')

            if query_word in the_word:
                word_result = the_word[query_word]
                messageBox.showinfo('Answer','The way you would say' + query_word + ' in the phonetic alphabet is ' +word_result + '!')
                engine.say('the way you would say'+query_country+'in the phonetic alphabet is'+result)
                engine.runAndWait()
                break

解决方法

我认为它对您不起作用的原因是您的 exe文件具有依赖性,因此您需要将其从dist文件夹中取出并从您的主文件夹中执行它项目,以便它可以访问依赖项

以下是一个链接,该链接应告诉您您需要了解的所有信息:PyInstaller