0

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. error trace

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`
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
Path- Or
  • 43
  • 1
  • 4
  • 13

1 Answers1

6

Python VLC needs external dependencies like the DLL files you see in the error. So you need to add them to your executable output with add-data.

Just copy all those *.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 dependency 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
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
  • 1
    hey , as you said i tried to add all required file and made exe . but when i ran the executable it run for a while ( executing the half part of program) and ended again with error `C:\Users\intel\Desktop>liveyoutbe.exe https://www.youtube.com/watch?v=9vMh9f41pqE https://www.youtube.com/watch?v=jVrB_vfS3AQ Traceback (most recent call last): File "liveyoutbe.py", line 34, in player = ins.media_player_new() AttributeError: 'NoneType' object has no attribute 'media_player_new' [5172] Failed to execute script liveyoutbe` – Path- Or Jul 08 '19 at 07:24
  • and seems that program ran for while but coludn't import vlc module still now ! – Path- Or Jul 08 '19 at 07:24
  • I tested this answer and worked for me. Remember that this solution is only for importing VLC. But it seems that you have errors on your code which mean it is a different problem. – Masoud Rahimi Jul 08 '19 at 07:40
  • but i have checked my code in my IDLE , it runs fine there without any error whereas the exe throws error. What could be possible reason ? – Path- Or Jul 08 '19 at 08:03
  • 3
    Thanks man!! Now it works fine , after adding the dll files i searched over web and found that i too needed to add the plugins folder of VLC and same you depicted in the answer , so instead of modifying the spec file , i did that with `--add-data` and finally code is on high . Again thanks for saving my time . I was totally confused – Path- Or Jul 08 '19 at 10:44
  • @MasoudRahimi Thank you so much for this answer. I mulled over past one week and finally, today your solution was exactly what I was needing for. But I still have one question, my exe file is now around 70MB in size due to the added VLC plugins, can I reduce it somehow? – Kartikeya May 13 '21 at 08:39
  • 1
    @Kartikeya If you have [upx](https://github.com/upx/upx) installed, PyInstaller would use it to compress the executable. – Masoud Rahimi May 13 '21 at 12:31
  • The fix of `AttributeError: 'NoneType' object has no attribute 'media_player_new'` for onefile for windows is `--add-data "./plugins/;./plugins/"` make sure that plugins is in the same path as main.py – bond benz Mar 11 '23 at 17:36