Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

python - Opening an EXE of my Pygame program gives me import errors

I made an MP3 player with pygame code:

from Tkinter import *
import pygame
import glob
import tkFont
songs=[]
for x in glob.glob('C:WhaleTunesDownloaded/*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:UsersP'saoDownloadsdistmp3player.exe:2: RuntimeWarning: import display: N
o module named _view
(ImportError: No module named _view)
C:UsersP'saoDownloadsdistmp3player.exe:2: RuntimeWarning: import draw: No m
odule named _view
(ImportError: No module named _view)
C:UsersP'saoDownloadsdistmp3player.exe:2: RuntimeWarning: import image: No
module named _view
(ImportError: No module named _view)
C:UsersP'saoDownloadsdistmp3player.exe:2: RuntimeWarning: import pixelcopy:
 No module named _view
(ImportError: No module named _view)
C:UsersP'saoDownloadsdistmp3player.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']
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...