【问题标题】:After pyinstaller--onefile creates the exe, the wav file in the program only plays on the computer that the exe file was created onpyinstaller--onefile创建exe后,程序中的wav文件只在创建exe文件的电脑上播放
【发布时间】:2020-11-05 05:06:27
【问题描述】:

'''
#import simpleaudio as sa
#from playsound import playsound
import winsound
from chart import chart
from BreakerZones import BreakerZones
import time
import sys
import colorama
import yaml  # to print the nested_lookup results(n) on separate lines
from nested_lookup import nested_lookup, get_all_keys # importing 2 items from nested_lookup
from colorama import Fore, Back, Style
import os

colorama.init(autoreset=True)  # If you don't want to print Style.RESET_ALL all the time,
                               # reset automatically after each print statement with True
print(colorama.ansi.clear_screen())

print('\n'*4)  # prints a newline 4 times


print(Fore.MAGENTA + '                             Arriving-' + Fore.GREEN + '    *** BREAKERVILLE USA ***')


def main():

    print('\n' * 2)
    print(Fore.BLUE + '   Breaker Numbers and Zones')

    k = get_all_keys(BreakerZones)
    # raw amount of keys even repeats , has quotes

    new_l = []                                         # eliminate extra repeating nested keys
    for e in k:                                        # has quotes
        if e not in new_l and sorted(e) not in new_l:  #
            new_l.append(e)                            #
    print()

    new_l.sort()  # make alphabetical
    newer_l = ('%s' % ', '.join(map(str, new_l)).strip("' ,"))  # remove ['%s'] brackets so they don't show up when run
    print(' ', yaml.dump(newer_l, default_flow_style=False))    #  strip("' ,")  or will see leading "' ," in output

    print(Fore.BLUE + '   ENTER A BREAKER # OR ZONE', Fore.GREEN + ': ', end='')
    i = input().strip().lower()  # these lines is workaround for the colorama
    print()              # user input() issue of 'code' appearing in screen output

    if i in k:
        n = (nested_lookup(i, BreakerZones, wild=False, with_keys=False))   # wild=True means key not case sensitive,
        print(yaml.dump(n, default_flow_style=False))                       # 'with_keys' returns values + keys also
        # for key, value in n.items():     eliminated by using yaml
        #    print(key, '--', value)       eliminated by using yaml
    else:
        print(Fore.YELLOW + ' Typo,' + Fore.GREEN + ' try again')
        main()

    print()

    print(Fore.GREEN + '           Continue? Y or N: C for breaker chart : ', end='')  # see comments ENTER A BREAKER
    ans = input().strip().lower()      # strip() removes any spaces before or after user input

    if ans == 'c':
        chart()
        print()
        print(Fore.GREEN + '           Continue? Y or N : ', end='')
        ans = input().strip().lower()             # strip() removes any spaces before or after user input
        if ans == 'y':                            # shorter version 'continue Y or N' after printing breaker chart
            main()
        else:
            print()
            print(Fore.MAGENTA + '                            Departing -' + Fore.GREEN + '   *** BREAKERVILLE ***')
        filename = os.path.join(getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__))),
                                'train_whistle.wav')
        #filename = 'C:/Users/oseda/PycharmProjects/FirstProject/train_whistle.wav'
        winsound.PlaySound(filename, winsound.SND_FILENAME)

        time.sleep(2) # delay to exit program
        sys.exit()

    elif ans != 'y':

        print()
        print(Fore.MAGENTA + '                            Good Day -' + Fore.GREEN + '   *** BREAKERVILLE ***')

        filename = os.path.join(getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__))),
                                'train_whistle.wav')
        #filename = 'C:/Users/oseda/PycharmProjects/FirstProject/train_whistle.wav'
        winsound.PlaySound(filename, winsound.SND_FILENAME)

        time.sleep(2)  # delay to exit program

        sys.exit()

    else:
        main()


main()
'''

【问题讨论】:

  • pyinstaller.readthedocs.io/en/stable/… 这个链接有一个很好的“测试代码”来检查我正在使用的应用程序是“捆绑”还是“源代码”。对于我列出的问题,这似乎是一个很好的测试。当我在我的脚本中运行它时,我得到:模块'sys'没有属性'frozen'。
  • 好的,我刚刚发现我必须使用 Pyinstaller 运行该测试。对我来说太棒了。我确实看到了“在 Pyinstaller 包中运行”的结果

标签: windows pyinstaller


【解决方案1】:

1.替换

filename = 'C:/Users/oseda/PycharmProjects/FirstProject/train_whistle.wav'

与

filename = os.path.join(getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__))),'train_whistle.wav')
  1. 将 C:/Users/oseda/PycharmProjects/FirstProject/train_whistle.wav 复制到与您的脚本相同的文件夹中
  2. 将 --add-data "train_whistle.wav;." 添加到您的 pyinstaller 构建命令中

【讨论】:

  • 'pyinstaller --onefile -add-data "train_whistle.wav;." BreakerVille.wav' 我看到错误:参数 -d/--debug: invalid selection: 'd-data (choose from 'all', 'imports', 'bootloader', 'noarchive')
  • 'pyinstaller --onefile -add-data "train_whistle.wav;." --add-data 'BreakerVille.wav;.' yourscript.py 是正确的语法。见pyinstaller.readthedocs.io/en/latest/…
  • 为了更清楚,程序与替换的行文件名=....一起工作得很好,我只是在使用pyinstaller时得到错误。
  • ' pyinstaller --onefile -add-data "train_whistle.wav;."BreakerVille.py" '
  • 我的解决方案中有一个错字(现已编辑):正确的命令是 pyinstaller --onefile --add-data "train_whistle.wav;." BreakerVille.py"
猜你喜欢
  • 1970-01-01
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多