Opening an EXE of my Pygame program gives me impor

2019-01-18 07:45发布

问题:

I made an MP3 player with pygame code:

from Tkinter import *
import pygame
import glob
import tkFont
songs=[]
for x in glob.glob('C:\WhaleTunes\Downloaded/*mp3'):

    songs.append(x)
Admin=Tk()
num=0
plpa=-1
songas=Label(Admin,text='',bg='red')
songas.place(relx=0.0,rely=0.7)
def play(number):

    pygame.mixer.music.unpause()
    pygame.mixer.music.load(songs[number])
    pygame.mixer.music.play()

    songas.configure(text=songs[number])


def pause():
     pygame.mixer.music.pause()
def Pre():
    global num
    if num == 0:
        z = len(songs)
        num=z
        num+=1
    num-=1
    play(num)
def Next():
    global num
    num+=1
    play(num)
#init pygame mixer
pygame.mixer.init()
#atach all buttons & labels
fons=tkFont.Font(family="bold", size=40)
fon=tkFont.Font(family="Helvetica", size=20)
tit=Label(Admin,text='Mp3 Player',font=fons,fg='grey',bg='red')
tit.place(relx=0.2,rely=0.0)
playnpause=Button(Admin,text='Play',command=lambda:play(num),fg='yellow',bg='red',font=fon)
playnpause.place(relx=0.0,rely=0.4)
last=Button(Admin,text='Previous',command=Pre,fg='yellow',bg='red',font=fon)
last.place(relx=0.2,rely=0.4)
first=Button(Admin,text='Next',command=Next,fg='yellow',bg='red',font=fon)
first.place(relx=0.5,rely=0.4)
pauses=Button(Admin,text='Pause',command=pause,fg='yellow',bg='red',font=fon)
pauses.place(relx=0.7,rely=0.4)
Admin.minsize(width=500, height=200)
Admin.maxsize(width=500, height=200)
Admin.configure(bg='red')
Admin.mainloop()

And I tried to put it into an exe with this code:

from distutils.core import setup
import py2exe

setup(console=['mp3player.py'])

When I run the mp3player.exe I get a bunch of import errors:

C:\Users\P'sao\Downloads\dist\mp3player.exe:2: RuntimeWarning: import display: N
o module named _view
(ImportError: No module named _view)
C:\Users\P'sao\Downloads\dist\mp3player.exe:2: RuntimeWarning: import draw: No m
odule named _view
(ImportError: No module named _view)
C:\Users\P'sao\Downloads\dist\mp3player.exe:2: RuntimeWarning: import image: No
module named _view
(ImportError: No module named _view)
C:\Users\P'sao\Downloads\dist\mp3player.exe:2: RuntimeWarning: import pixelcopy:
 No module named _view
(ImportError: No module named _view)
C:\Users\P'sao\Downloads\dist\mp3player.exe:2: RuntimeWarning: import transform:
 No module named _view
(ImportError: No module named _view)

Anyone know how to fix this?

And when I compile everything I get this error:

The following modules appear to be missing
['AppKit', 'Foundation', 'Numeric', 'OpenGL.GL', '_scproxy', 'copyreg', 'dummy.P
rocess', 'numpy', 'pkg_resources', 'queue', 'winreg', 'pygame.sdlmain_osx']

回答1:

The solution is to add import pygame._view to the top of your main source file. Any of the packagers should work after that. I encountered this problem using cx_Freeze, py2exe, and pyInstaller. This is a serious bug affecting many of the exe packagers when attempting to package pygame programs.



回答2:

Had the same problem before and found answer to solve it by myself:

After few weeks (had this problem even before) I'm happy to say that I solved this problem! :)

1st part of my problem (http://i.stack.imgur.com/WpkjR.png): I solved it by editing setup.py script with adding "excludes" part in it. That resulted in successful making of executable file!

Modified setup.py script:

from distutils.core import setup
import py2exe
setup(windows=['source_static.py'], options={
          "py2exe": {
              "excludes": ["OpenGL.GL", "Numeric", "copyreg", "itertools.imap", "numpy", "pkg_resources", "queue", "winreg", "pygame.SRCALPHA", "pygame.sdlmain_osx"],
              }
          }
      )

So, if you have similar issues, just put those "missing" modules into this "excludes" line.

2nd part:

After I succeeded in making of executable file, I had next problem: "The application has requested the Runtime to terminate it in unusual way. Please contact...". After days and days of searching and thinking how to solve this another problem, I found a way to do it. I couldn't believe that the problem was so absurd. The problem was in my code, with font definition:

font1 = pygame.font.SysFont(None, 13)

After changing "None" to some system font name (for an example "Arial" (must be a string)), and compiling, I couldn't believe that my .exe file worked!

font1 = pygame.font.SysFont("Arial", 13)

Of course, you can use your own font, but you must specify its path and define it in your program.

So for all of you who are experiencing this issues, try this steps and I hope that you will succeed. I really hope that this will help you, because I've lost days and weeks trying to solve these problems. I even tried making my .exe file with all versions of python and pygame, with many other .exe builders and setup scripts, but without luck. Besides these problems, I had many other problems before but I found answers to them on stackoverflow.com.

I'm happy that I found a way to solve this problems and to help you if you are faced with the same ones.

Small tips (things I've also done):

1st: update your Microsoft Visual C++ library to the latest one.

2nd: if you have images or fonts similar that your executable program needs, include them to dist folder (where your .exe file has been created).

3rd: when you are making your .exe file, include all needed files to the folder where your setup.py script is (all files and directories that your main script uses).

Used Python 2.7 x64, pygame and py2exe.