1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] PyInstaller Executable fails to run download operation with external script despite...

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 08:22.

  1. Stack

    Stack Membro Participativo

    I'm trying to compile a python script into an executable, but I can't properly include the external file tg-upload.py that takes care of the download operations

    The relevant code in my telethongram.py is this

    def download_thread():
    while True:
    link = download_queue.get()
    if link is None:
    break # Termina il thread
    download_link_script(link)
    download_queue.task_done()

    download_thread = Thread(target=download_thread)
    download_thread.start()

    def download_link_script(link):
    print(f'Chiamato download_link_script con il link: https://stackoverflow.com/questions/79033825/pyinstaller-executable-fails-to-run-download-operation-with-external-script-desp')

    # Ottieni il percorso del file tg-upload.py
    if getattr(sys, 'frozen', False):
    # Se l'applicazione è congelata, ottieni il percorso del file dal bundle
    tg_upload_path = os.path.join(sys._MEIPASS, 'tg-upload.py')
    else:
    # Se l'applicazione non è congelata, usa il percorso relativo
    tg_upload_path = 'tg-upload.py'

    print(f'Percorso tg-upload.py: {tg_upload_path}')

    command = [sys.executable, tg_upload_path, '--profile', 'enteSoffitta', '--dl', '--links'] + link.split()
    print(f'Comando eseguito: {" ".join(command)}')

    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    progress_updates = [] # Create an array to hold the progress updates
    while True:
    output = process.stdout.readline()
    if output == b'' and process.poll() is not None:
    break
    if output:
    try:
    output = output.decode('utf-8') # Decode the bytes to a string
    except UnicodeDecodeError:
    output = output.decode('latin-1') # Fallback to latin-1 decoding
    print(output.strip())
    match = re.search(r'(\d+(\.\d+)?%)', output.strip()) # Extract the progress percentage
    if match:
    progress_updates.append(match.group(1)) # Add progress update to array

    # Cattura e stampa l'output di errore
    stderr_output = process.stderr.read()
    if stderr_output:
    try:
    stderr_output = stderr_output.decode('utf-8')
    except UnicodeDecodeError:
    stderr_output = stderr_output.decode('latin-1')
    print(f'Errore: {stderr_output}')

    rc = process.poll()
    return jsonify(progress_updates) # Send progress updates as JSON

    @app.route('/scarica', methods=['GET'])
    def scarica():
    print('Ricevuta richiesta GET a /scarica')
    link = request.args.get('message_link')
    if link:
    response = download_link_script(link) # Get the Response object
    return response # Return the Response object directly
    else:
    return jsonify({'error': 'Nessun message_link fornito'}), 400

    @app.route('/scarica_multi', methods=['POST'])
    def scarica_multi():
    print('Ricevuta richiesta POST a /scarica_multi')
    links = request.json.get('message_link')
    if links:
    for link in links:
    download_queue.put(link) # Aggiungi ogni link alla coda
    return jsonify({'message': 'Download iniziati'})
    else:
    return jsonify({'error': 'Nessun message_link fornito'}), 400

    if __name__ == '__main__':
    with app.app_context():
    db.create_all()
    app.run(debug=False)
    download_queue.put(None)
    download_thread.join()


    To compile in windows I'm trying to include the file in this way

    pyinstaller --onefile --add-data "tg-upload.py:." --add-data "static:static" --add-data "templates:templates" telethongram.py


    but the problem is that even if it is included, then the download operations do not start with the telethongram.exe executable, while with the pure python script I have no problems downloading with tg-upload.py There is a problem importing tg-upload.py into the telethongram.exe executable Also under linux problem is the same

    Continue reading...

Compartilhe esta Página