0

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

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

现在这是完整的错误跟踪

`Traceback (most recent call last):
 File "site-packages\PyInstaller\loader\pyiboot01_bootstrap.py", line   149, in _
 _init__
 File "ctypes\__init__.py", line 348, in __init__
 OSError: [WinError 126] The specified module could not be found

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
 File "liveyoutbe.py", line 2, in <module>
 File "<frozen importlib._bootstrap>", line 961, in _find_and_load
 File "<frozen importlib._bootstrap>", line 950, in  _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
 File "c:\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
 line 627, in exec_module
 exec(bytecode, module.__dict__)
 File "site-packages\vlc.py", line 207, in <module>
 File "site-packages\vlc.py", line 163, in find_lib
 File "site-packages\PyInstaller\loader\pyiboot01_bootstrap.py", line  151, in _
_init__
 __main__.PyInstallerImportError: Failed to load dynlib/dll 'libvlc.dll'. Most pr
 obably this dynlib/dll was not found when the application was frozen.
 [6032] Failed to execute script liveyoutbe`
4

1 回答 1

6

Python VLC 需要外部依赖项,例如您在错误中看到的 DLL 文件。所以你需要将它们添加到你的可执行输出中add-data

*.dll只需将当前 VLC 安装路径(例如)中的所有内容复制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 路径中的整个插件目录添加到可执行输出即可。为此,在执行上述命令后,您将获得一个添加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
于 2019-07-07T17:41:11.097 回答