Search code examples
pythonherokusubprocess

subprocess.run throws not found error heroku when opening exe file


I'm trying to use youtube-dl to download videos from youtube and I'm using subprocess.run for that purpose

the code has to open youtube-dl.exe with an argument(link of the video)

import subprocess

link = 'https://www.youtube.com/watch?v=k4iMyMQTpeo'
subprocess.run(['youtube-dl.exe',link], shell=True)

everything works fine when I run locally but when I run it on Heroku it gives me this error

2021-07-21T02:20:03.534926+00:00 heroku[web.1]: Process exited with status 0
2021-07-21T02:20:03.699504+00:00 heroku[web.1]: State changed from starting to crashed
2021-07-21T02:20:03.447886+00:00 app[web.1]: https://www.youtube.com/watch?v=k4iMyMQTpeo: 1: youtube-dl.exe: not found

What i have tried

i tried to run it using PowerShell

subprocess.run(['powershell','youtube-dl.exe',link],shell=True)

but it gave a similar-looking error

2021-07-20T22:10:32.167473+00:00 heroku[web.1]: Starting process with command `python3 main.py`
2021-07-20T22:10:38.880962+00:00 app[web.1]: ./youtube-dl.exe: 1: powershell: not found

i tried to changing shell to false but it didnt work and i tried to list directories to make sure youtube-dl.exe is there

2021-07-21T03:28:18.106973+00:00 app[web.1]: ['youtube-dl.exe', 'requirements.txt', '.profile.d', 'UNILAD responds!-k4iMyMQTpeo.f248.webm.part', '.heroku', 'Procfile', 'main.py', 'runtime.txt']
2021-07-21T03:28:18.114136+00:00 app[web.1]: Traceback (most recent call last):
2021-07-21T03:28:18.114144+00:00 app[web.1]:   File "/app/main.py", line 13, in <module>
2021-07-21T03:28:18.114470+00:00 app[web.1]:     subprocess.run(['youtube-dl.exe',link], shell=False)
2021-07-21T03:28:18.114506+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/subprocess.py", line 505, in run
2021-07-21T03:28:18.114919+00:00 app[web.1]:     with Popen(*popenargs, **kwargs) as process:
2021-07-21T03:28:18.114952+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/subprocess.py", line 951, in __init__
2021-07-21T03:28:18.115812+00:00 app[web.1]:     self._execute_child(args, executable, preexec_fn, close_fds,
2021-07-21T03:28:18.115843+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/subprocess.py", line 1821, in _execute_child
2021-07-21T03:28:18.117287+00:00 app[web.1]:     raise child_exception_type(errno_num, err_msg, err_filename)
2021-07-21T03:28:18.117408+00:00 app[web.1]: FileNotFoundError: [Errno 2] No such file or directory: 'youtube-dl.exe'

Solution

  • You do not need to use subprocess module, you can use youtube-dl directly.

    Please install youtube_dl:

    pip3 install youtube_dl
    

    Then you can download videos, for example:

    from __future__ import unicode_literals
    import youtube_dl
    
    ydl_opts = {}
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download(['https://www.youtube.com/watch?v=BaW_jenozKc'])
    

    Finally, deploy it to Heroku.