问题
I tried to make a standalone executable of my python script with auto-py-to-exe , it basically provides easy interface for creating executable with pyinstaller, so I made my python script's exe, it's console based and when I try to run the exe that I made of my script, then console opens up for a second a closes with bunch of errors quickly. However, the script runs fine in idle. I had attached a screenshot of the error I received. As per my observations most probably the error is occurring due to the import of VLC module as in error trace you will see it occurred from line 2 and that line I had imported VLC. I have also observed by changing the line number of import for VLC. I am quite a beginner, So I need to know the steps for the solution.
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
now here is the complete error trace
`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`
回答1:
Python VLC needs external dependencies like the DLL you see in the error. So you need to add them to your executable output with add-data.
Just copy all *.dll
inside your current VLC install path (e.g C:\Program Files\VideoLAN\VLC
) besides to your script and use below command to generate your executable:
pyinstaller.exe -F --add-data "./libvlc.dll;." --add-data "./axvlc.dll;." --add-data "./libvlccore.dll;." --add-data "./npvlc.dll;." script.py
Edit: It seems that you still need one more dependecy which is plugins
directory. Just add the whole plugins directory in your VLC path to your executable output. For that after executing above command you would get a spec file add a.datas += Tree('<path_to_vlc_plugins_dir>', prefix='plugins')
to the file like this:
# -*- 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 )
Finally, execute this:
pyinstaller script.spec
来源:https://stackoverflow.com/questions/56919875/error-while-running-the-exe-file-made-with-pyinstaller