Search code examples
pythonpygamecx-freeze

How to fix "ImportError: cannot import name 'setup' form 'cx_Freeze' "


I'm sorry not to use English well.

I made a game with Python 3.7 and pygame.

I tried to build an exe with cx_Freeze version 5.1.1 but an ImportError happens...

Someone uploaded a question with the same error but not resolved: cx_Freeze- ImportError: cannot import name setup.

I tried to change from cx_Freeze import setup, Executable to import cx_Freeze and then use cx_Freeze.setup and cx_Freeze.Executable, but then an error setup not in cx_Freeze happens...

This is my install.py code

from cx_Freeze import setup, Executable
import cx_Freeze
setup(
    name = "hello",
    options={"build_exe": {"packages":["pygame"],
                           "included_files":["UFO.bmp","UFO_Monster.bmp","boss_Atack.jpg","boss.png","background_1.png","bullet.png","boom.png","shot.wav","explosion.wav","mybgm.wav","D2coding.ttf","Meteor.png"]}},
    version = "0.1",
    executables = [
    cx_Freeze.Executable('Main.py'),
    cx_Freeze.Executable('Boss_class.py'),
    cx_Freeze.Executable('Boss_class_Attack.py'),
    cx_Freeze.Executable('Meteor.py'),
    cx_Freeze.Executable('UFO_class.py'),
    cx_Freeze.Executable('UFO_Monster.py')
]
)

This is the error message :

Traceback (most recent call last): File "cx_Freeze.py", line 2, in from cx_Freeze import setup, Executable File "C:\Users```\source\repos\PythonGame\cx_Freeze.py", line 2, in from cx_Freeze import setup, Executable ImportError: cannot import name 'setup' from 'cx_Freeze' (C:\Users```\source\repos\PythonGame\cx_Freeze.py)


Solution

  • Do you have a file named cx_Freeze.py directly in your source directory, instead of importing through a package directory and possibly through the one installed with pip?

    The path shows cx_Freeze.py and not the expected path from an installed package.