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
199 views
in Technique[技术] by (71.8m points)

python - Pyinstaller with pygame

I have tried multiple times to use PyInstaller to bundle my Python2 programs, but it never seems to work with the Pygame module. I have seen many other issues on this topic, but I couldn't find any useful answers.

Does anybody know a solution to this?

I am trying to do this on Ubuntu 14.10.

-Edit-

To be more specific:

"never seems to work" means that when I run PyInstaller and build the application it appears to work and makes the application. When you try to run the application nothing happens.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I searched a lot in the PyInstaller doc to get my own game working. I don't know much about Ubuntu, but I got everything working in Windows and it should be very similar. The key here is to get PyInstaller to package your ressources (images, sounds, etc.) with your Python code. The best distribution is to use the pyinstaller --onefile main.py console command in your directory to get a single executable. PyInstaller will make a dist folder and a main.spec file. The .spec file contains all the options PyInstaller uses for making your executable. For a --onefile executable, it should look like this:

# -*- mode: python -*-

block_cipher = None

a = Analysis(['..\src\bitmessagemain.py'],
         pathex=['C:\example\pyinstaller\bitmessagemain'],
         binaries=None,
         datas=None,
         hiddenimports=[],
         hookspath=None,
         runtime_hooks=None,
         excludes=None,
         win_no_prefer_redirects=None,
         win_private_assemblies=None,
         cipher=block_cipher)

pyz = PYZ(a.pure, a.zipped_data,
         cipher=block_cipher)
exe = EXE(pyz,
      a.scripts,
      a.binaries,
      a.zipfiles,
      a.datas,
      a.binaries, 'BINARY')],
      name='bitmessagemain',
      debug=False,
      strip=None,
      upx=True,
      console=False , icon='src\images\can-icon.ico')

Be sure to run PyInstaller once to have it create the .spec file, even if the resulting executable doesn't work.

a is an object that collects stuff to put in your executable. You want to add your ressource folders in the datas= part. To do this, create a list:

added_files = [
     ( 'data', 'data' ),
     ( 'sfx/*.mp3', 'sfx' ),
     ( 'src/README.txt', '.' )
     ]

The first line in this example adds all the content of the folder data located in the same folder as you main.spec and main.py and adds it to the data folder inside the executable. You want to keep your paths working, so the two entries of the tuple should usually be the same. The second line adds all .mp3 files from sfx to the sfx folder inside the exectuable, and so on. Add all your ressources in this list, and be careful to keep your paths valid.

You can then modify your .spec (which is standard python code):

# -*- mode: python -*-

block_cipher = None

    added_files = [     #Any variable name works
     ( 'data', 'data' ),
     ( 'sfx/*.mp3', 'sfx' ),
     ( 'src/README.txt', '.' )
     ]

a = Analysis(['..\src\bitmessagemain.py'],
         pathex=['C:\example\pyinstaller\bitmessagemain'],
         binaries=None,
         datas=added_files,    #Dont forget to change this line!
         hiddenimports=[],
         hookspath=None,
         runtime_hooks=None,
         excludes=None,
         win_no_prefer_redirects=None,
         win_private_assemblies=None,
         cipher=block_cipher)
...

The last very important step is to add those two lines of code at the start of your main.py:

if getattr(sys, 'frozen', False):
    os.chdir(sys._MEIPASS)

The trick here is that when you run your executable, PyInstaller will unpack all your data files in a hidden _MEIPASS folder. You want to set your current directory to this folder so that your code finds your ressources and your paths stays valid.

Finally, I would advise you to use the os.path.join function for all your paths in your code. It'll make the paths portable to other platforms.

On a side note, if you use the --onedir option, do the same thing but add the two lines:

if getattr(sys, 'frozen', False):
    os.chdir(os.path.dirname(sys.executable))

If you do this and your executable closes immediately when double clicking (in Windows), you need to change the console=False to console=True line and then type cmd /k COMPLETEPATHTOYOUREXECUTABLE in the search field in the StartMenu. This will run your .exe and keep the console open so you can see the error log. It will probably be asking for some file you forgot to include, that way you can add it to the .spec, rebuild your .exe with pyinstaller main.spec and try again.

Hope this helps others too, the info on the web is quite obscure for PyInstaller+Pygame.


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

...