运行 使用 pyinstaller 制作的 exe 文件时出错
Error while running the exe file made with pyinstaller
我试图用 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`
Python VLC 需要外部依赖项,例如您在错误中看到的 DLL 文件。因此,您需要使用 add-data
.
将它们添加到可执行输出中
除了脚本之外,只需复制当前 VLC 安装路径(例如 C:\Program Files\VideoLAN\VLC
)中的所有 *.dll
,然后使用以下命令生成可执行文件:
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
我试图用 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`
Python VLC 需要外部依赖项,例如您在错误中看到的 DLL 文件。因此,您需要使用 add-data
.
除了脚本之外,只需复制当前 VLC 安装路径(例如 C:\Program Files\VideoLAN\VLC
)中的所有 *.dll
,然后使用以下命令生成可执行文件:
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