Home > other >  pyinstaller is not attaching my image when I create an executable
pyinstaller is not attaching my image when I create an executable

Time:10-21

How can i make an executable that can load image, and that my friends don't depend on downloading the executable and image or a heavy folder? I'm trying to create an executable for my python program, but when I try to open its executable, I get this error:

Traceback (most recent call last):
  File "main.py", line 49, in <module>
  File "tkinter\__init__.py", line 4103, in __init__
  File "tkinter\__init__.py", line 4048, in __init__
_tkinter.TclError: couldn't open "logo.png": no such file or directory

The code I'm using to compile is this:

pyinstaller --onefile --windowed --add-data "logo.png;." main.py

Is there any other compilation method or other way to make an executable with the logo attached? I tried some methods like:

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
    return os.path.join(base_path, relative_path)

But to no avail, and my complete code is this:

from pytube import Playlist, YouTube
from tkinter import *
from moviepy import *
import os
import re

if os.name == "nt":
    downfolder = f"{os.getenv('USERPROFILE')}\\Downloads"
else:
    downfolder = f"{os.getenv('HOME')}/Downloads"

def playlist_audio():
    get_link = link_field.get()
    p = Playlist(get_link)
    p._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")
    for video in p.videos:
     screen.title('downloading.')
     mp3_audio = video.streams.filter(only_audio=True).first().download(downfolder)
     audioconvert(mp3_audio)
             
    #
    screen.title('completed')

def video_mp3():
    get_link = link_field.get()
    screen.title('downloading.')
    mp3_audio = YouTube(get_link).streams.filter(only_audio=True).first().download(downfolder)
    audioconvert(mp3_audio)

    #
    screen.title('completed')

def audioconvert(mp3_audio):
   try:
     base, ext = os.path.splitext(mp3_audio)
     new_file = base   '.mp3'
     os.rename(mp3_audio , new_file)
   except WindowsError:
     os.remove(new_file)
     os.rename(mp3_audio , new_file)

screen = Tk()
title = screen.title('Python mp3')
screen.resizable(False, False)
Canvas = Canvas(screen, width=400, height=200)
Canvas.pack()

logoimg = PhotoImage(file='logo.png')
logo_img = logoimg.subsample(2, 2)
Canvas.create_image(215, 25, image=logo_img)

link_field = Entry(screen, width=50)
link_label = Label(screen, text="Enter the song/playlist URL.")

Canvas.create_window(200, 90, window=link_field)
Canvas.create_window(200, 70, window=link_label)

def convert():
  try:
      video_mp3()
      return True
  except:
      screen.title("wait a few seconds")

  try:
      playlist_audio()
      return False
  except:
      screen.title("unknown error, try again")

download = Button(screen, text="download", bg='green', padx='20', pady='5',font=('Arial', 12), fg='#fff', command=convert)
Canvas.create_window(200, 140, window=download)

screen.mainloop()

CodePudding user response:

I was able to get this to work.

In your python code add this to access your image from the MEI directory

photopath = os.path.join(os.path.dirname(__file__), 'logo.png')
logoimg = PhotoImage(file=photopath)

Here are the steps I took.

  1. create empty folder; cd into it; copy the logo.png an script(main.py)
  2. py -m venv venv && venv\Scripts\activate
  3. py -m pip install --upgrade pip pyinstaller moviepy pytube
  4. pyinstaller -F main.py
  5. Then in the spec file add on line 11 (i think) edit datas=[('logo.png','.')],

You can make other adjustments in the spec file as well like setting console=False for it to work in windowed mode and changing the name of the output executable file and other stuff.

  1. pyinstaller main.spec

Thats it... the executable should work fine and load your photo and all.

  • Related