运行pyinstaller生成的exe文件时出错

时间:2019-07-07 06:31:22

标签: python pyinstaller python-vlc

我尝试使用auto-py-to-exe来制作python脚本的独立可执行文件,它基本上为使用pyinstaller创建可执行文件提供了简单的界面,因此我制作了python脚本的exe,它基于控制台,并且在我尝试运行我用脚本制作的exe,然后控制台打开一秒钟,然后迅速关闭,并出现一堆错误。但是,该脚本在空闲状态下运行良好。 我已附上我收到的错误的屏幕截图。根据我的观察,错误很可能是由于导入VLC模块而导致的,因为在错误跟踪中,您会看到它是从第2行发生的,而我已经导入了VLC。我还通过更改VLC的导入行数来观察。 我是一个初学者,所以我需要知道解决方案的步骤。 error trace

O(n)

现在这是完整的错误跟踪

from bs4 import BeautifulSoup
import vlc
import pafy
import urllib.request
import time


textToSearch = 'tremor dimitri vegas ' 
query = urllib.parse.quote(textToSearch)
urlq = "https://www.youtube.com/results?search_query=" + query
response = urllib.request.urlopen(urlq)
html = response.read()
soup = BeautifulSoup(html, 'html.parser')
track_links=[]
i=0
for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}):
    i=i+1
    print('https://www.youtube.com' + vid['href'])
    track_links.append('https://www.youtube.com' + vid['href'])
    if i==2:
        break
print()


url = track_links[1]
video = pafy.new(url)
best = video.getbestaudio()
playurl = best.url
ins = vlc.Instance()
player = ins.media_player_new()

code = urllib.request.urlopen(url).getcode()
if str(code).startswith('2') or str(code).startswith('3'):
    print('Stream is working and playing song')
else:
    print('Stream is dead')

Media = ins.media_new(playurl)
Media.get_mrl()
player.set_media(Media)
player.play()

time.sleep(20)
player.stop()#breaking here just for check purpose

1 个答案:

答案 0 :(得分:1)

Python VLC需要外部依赖性,例如您在错误中看到的DLL。因此,您需要使用add-data将它们添加到可执行输出中。

只需将当前VLC安装路径(例如*.dll)内的所有C:\Program Files\VideoLAN\VLC复制到脚本中,然后使用以下命令生成可执行文件:

pyinstaller.exe -F --add-data "./libvlc.dll;." --add-data "./axvlc.dll;." --add-data "./libvlccore.dll;." --add-data "./npvlc.dll;." script.py

编辑:看来您仍然需要一个依赖项,即plugins目录。只需将VLC路径中的整个plugins目录添加到可执行输出即可。为此,执行上述命令后,您将获得一个规格文件,将a.datas += Tree('<path_to_vlc_plugins_dir>', prefix='plugins')添加到该文件中,如下所示:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['script.py'],
             pathex=['<root_project_path>'],
             binaries=[],
             datas=[('./libvlc.dll', '.'), ('./axvlc.dll', '.'), ('./libvlccore.dll', '.'), ('./npvlc.dll', '.')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)

a.datas += Tree('<path_to_vlc_plugins_dir>', prefix='plugins')
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='script',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

最后,执行此:

pyinstaller script.spec
相关问题