简体   繁体   中英

Converting pygame file to exe with cx_Freeze

So i wanted to make an .exe file from a small game but when i try to build it, when i type:

setup.py build

it gives and error that pygame is not a module

C:\Python33\Projects>setup.py build
running build
running build_exe
Traceback (most recent call last):
  File "C:\Python33\Projects\setup.py", line 11, in <module>
    executables = executables
  File "C:\Python34\lib\site-packages\cx_Freeze\dist.py", line 362, in setup
    distutils.core.setup(**attrs)
  File "C:\Python34\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Python34\lib\distutils\dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "C:\Python34\lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "C:\Python34\lib\distutils\command\build.py", line 126, in run
    self.run_command(cmd_name)
  File "C:\Python34\lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "C:\Python34\lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "C:\Python34\lib\site-packages\cx_Freeze\dist.py", line 232, in run
    freezer.Freeze()
  File "C:\Python34\lib\site-packages\cx_Freeze\freezer.py", line 619, in Freeze

    self.finder = self._GetModuleFinder()
  File "C:\Python34\lib\site-packages\cx_Freeze\freezer.py", line 378, in _GetMo
duleFinder
    finder.IncludePackage(name)
  File "C:\Python34\lib\site-packages\cx_Freeze\finder.py", line 686, in Include
Package
    module = self._ImportModule(name, deferredImports)
  File "C:\Python34\lib\site-packages\cx_Freeze\finder.py", line 386, in _Import
Module
    raise ImportError("No module named %r" % name)
ImportError: No module named 'pygame'

C:\Python33\Projects>

My setup file looks like this:

import cx_Freeze

executables = [cx_Freeze.Executable("racegame.py")]

cx_Freeze.setup(
    name="Racegame",
    options={"build_exe": {"packages": ["pygame"],
                          "include_files": ["racecar.png"]}},
    description="A race game",

    executables = executables
    )
from cx_Freeze import setup,Executable
setup(name="NAME OF YOUR PROGRAM",
         version="1.0",
         description="as above",
         executables=[Executable("NAME OF THE SCRIPT.py")])

Use this, I always executing with this.Copy-paste this codes to setup.py.And then you have to put that special files into your folder.Afer you executed it, drag your png files to folder into same folder with your program.

So open the terminal (cmd) in the same directory with your program, write;

python setup.py build

Then put your png,jpg etc. files in to that folder. That's it.

Here I record a video for you. In the folder that setup.py and your script stays,click shift+right-click and open the terminal in there. For example I drag the apple.png in video, so you must put the all special files where I put in video.

PLEASE BE SURE your script starting with #!/usr/bin/env python . Otherwise Python can not find the Python directory and throws that error no module etc. ALWAYS start your scripts with that.

Example:

#!/usr/bin/env python
import pygame,random
import ChuckNorris

pygame.init()
#codes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM