40

I've created my setup.py file as instructed but I don't actually.. understand what to do next. Typing "python setup.py build" into the command line just gets a syntax error.

So, what do I do?

setup.py:

from cx_Freeze import setup, Executable

setup(
    name = "On Dijkstra's Algorithm",
    version = "3.1",
    description = "A Dijkstra's Algorithm help tool.",
    exectuables = [Executable(script = "Main.py", base = "Win32GUI")])
Edwin
  • 552
  • 1
  • 5
  • 12
  • 1
    Can you show us the `setup.py`, your python version? – wkl Mar 27 '12 at 18:47
  • 2
    what is the syntax error? Can you post a traceback? – aquavitae Mar 27 '12 at 19:00
  • @aquavitae It's in the command line. Can someone please make it clear where I'm supposed to put "python setup.py build" because the instructions I've read do not make it clear. – Edwin Mar 27 '12 at 19:03
  • 4
    I think your problem is `exectuables` is misspelled. It should be `executables`. – wkl Mar 27 '12 at 19:08
  • @birryree: Oh… whoops. Okay, I'll give it another go. – Edwin Mar 27 '12 at 19:14
  • You run `python setup.py build` from the system command line (command prompt, terminal), not from a Python shell. – Thomas K Mar 28 '12 at 17:07
  • @aquavitae: I managed to grab this screenshot: http://upload.shaiex.net/files/139/cxfreezeerror.png – Edwin Mar 28 '12 at 18:04

6 Answers6

36
  • Add import sys as the new topline
  • You misspelled "executables" on the last line.
  • Remove script = on last line.

The code should now look like:

import sys
from cx_Freeze import setup, Executable

setup(
    name = "On Dijkstra's Algorithm",
    version = "3.1",
    description = "A Dijkstra's Algorithm help tool.",
    executables = [Executable("Main.py", base = "Win32GUI")])

Use the command prompt (cmd) to run python setup.py build. (Run this command from the folder containing setup.py.) Notice the build parameter we added at the end of the script call.

Bryan
  • 361
  • 3
  • 2
  • 2
    What is the purpose of base, and what if I want my executable to run across multiple platforms? – Max Jul 15 '16 at 20:28
15

I'm really not sure what you're doing to get that error, it looks like you're trying to run cx_Freeze on its own, without arguments. So here is a short step-by-step guide on how to do it in windows (Your screenshot looks rather like the windows command line, so I'm assuming that's your platform)

  1. Write your setup.py file. Your script above looks correct so it should work, assuming that your script exists.

  2. Open the command line (Start -> Run -> "cmd")

  3. Go to the location of your setup.py file and run python setup.py build

Notes:

  1. There may be a problem with the name of your script. "Main.py" contains upper case letters, which might cause confusion since windows' file names are not case sensitive, but python is. My approach is to always use lower case for scripts to avoid any conflicts.

  2. Make sure that python is on your PATH (read http://docs.python.org/using/windows.html)1

  3. Make sure are are looking at the new cx_Freeze documentation. Google often seems to bring up the old docs.

aquavitae
  • 17,414
  • 11
  • 63
  • 106
  • "'python' is not recognized as an internal or external command, operable program or batch file." – Edwin Mar 29 '12 at 17:26
  • "python: can't open file 'setup.py': [Errno 2] No such file or directory" Typing python in gets the Python command prompt as expected so I don't understand that error. My file is in the Python31 folder. – Edwin Mar 29 '12 at 19:20
  • Use `cd` to change to the directory where your `setup.py` file is located. – Thomas K Mar 29 '12 at 21:24
  • 1
    @ThomasK: That did it! I've never actually used the command line before so I need guiding through it like a little kid. Thanks for the help everyone. – Edwin Mar 30 '12 at 17:05
8

I ran into a similar issue. I solved it by setting the Executable options in a variable and then simply calling the variable. Below is a sample setup.py that I use:

from cx_Freeze import setup, Executable
import sys

productName = "ProductName"
if 'bdist_msi' in sys.argv:
    sys.argv += ['--initial-target-dir', 'C:\InstallDir\\' + productName]
    sys.argv += ['--install-script', 'install.py']

exe = Executable(
      script="main.py",
      base="Win32GUI",
      targetName="Product.exe"
     )
setup(
      name="Product.exe",
      version="1.0",
      author="Me",
      description="Copyright 2012",
      executables=[exe],
      scripts=[
               'install.py'
               ]
      ) 
Cesar
  • 423
  • 5
  • 14
8

You can change the setup.py code to this:

    from cx_freeze import setup, Executable
    setup( name = "foo",
           version = "1.1",
           description = "Description of the app here.",
           executables = [Executable("foo.py")]
         )

I am sure it will work. I have tried it on both windows 7 as well as ubuntu 12.04

Pratik Singhal
  • 6,283
  • 10
  • 55
  • 97
3

find the cxfreeze script and run it. It will be in the same path as your other python helper scripts, such as pip.

cxfreeze Main.py --target-dir dist

read more at: http://cx-freeze.readthedocs.org/en/latest/script.html#script

gcb
  • 13,901
  • 7
  • 67
  • 92
-1

I usually put the calling setup.py command into .bat file to easy recall. Here is simple code in COMPILE.BAT file:

python setup.py build
@ECHO:
@ECHO    . : ` . *   F I N I S H E D   * . ` : . 
@ECHO:
@Pause

And the setup.py is organized to easy customizable parameters that let you set icon, add importe module library:

APP_NAME     = "Meme Studio";      ## < Your App's name
Python_File  = "app.py";     ## < Main Python file to run
Icon_Path    = "./res/iconApp48.ico"; ## < Icon
UseFile      = ["LANGUAGE.TXT","THEME.TXT"];
UseAllFolder = True; ## Auto scan folder which is same level with Python_File and append to UseFile.
Import       = ["infi","time","webbrowser",   "cv2","numpy","PIL","tkinter","math","random","datetime","threading","pathlib","os","sys"];  ## < Your Imported modules (cv2,numpy,PIL,...)

Import+=["pkg_resources","xml","email","urllib","ctypes",   "json","logging"]
################################### CX_FREEZE IGNITER ###################################

from os import walk
def dirFolder(folderPath="./"): return next(walk(folderPath), (None, None, []))[1];  # [ Folder ]
def dirFile(folderPath="./"): return next(walk(folderPath), (None, None, []))[2];  # [ File ]
if UseAllFolder: UseFile += dirFolder();

import sys, pkgutil;
from cx_Freeze import setup, Executable;

BasicPackages=["collections","encodings","importlib"] + Import;
def AllPackage(): return [i.name for i in list(pkgutil.iter_modules()) if i.ispkg]; # Return name of all package
#Z=AllPackage();Z.sort();print(Z);
#while True:pass;
def notFound(A,v): # Check if v outside A
    try: A.index(v); return False;
    except: return True;
build_msi_options = {
    'add_to_path': False,
    "upgrade_code": "{22a35bac-14af-4159-7e77-3afcc7e2ad2c}",
    "target_name": APP_NAME,
    "install_icon": Icon_Path,
    'initial_target_dir': r'[ProgramFilesFolder]\%s\%s' % ("Picox", APP_NAME)
}
build_exe_options = {
    "includes": BasicPackages,
    "excludes": [i for i in AllPackage() if notFound(BasicPackages,i)],
    "include_files":UseFile,
    
    "zip_include_packages": ["encodings"] ##
}
setup(  name = APP_NAME,
        options = {"build_exe": build_exe_options},#"bdist_msi": build_msi_options},#,  
        executables = [Executable(
            Python_File,
            base='Win32GUI',#Win64GUI
            icon=Icon_Path,
            targetName=APP_NAME,
            copyright="Copyright (C) 2900AD Muc",
            )]
);

The modules library list in the code above is minimum for workable opencv + pillow + win32 application.

Example of my project file organize:

enter image description here

========== U P D A T E ==========

Although cx_Freeze is a good way to create setup file. It's really consume disk space if you build multiple different software project that use large library module like opencv, torch, ai... Sometimes, users download installer, and receive false positive virus alert about your .exe file after install.

Thus, you should consider use SFX archive (.exe) your app package and SFX python package separate to share between app project instead.

  • You can create .bat that launch .py file and then convert .bat file to .exe with microsoft IExpress.exe.
  • Next, you can change .exe icon to your own icon with Resource Hacker: http://www.angusj.com/resourcehacker/
  • And then, create SFX archive of your package with PeaZip: https://peazip.github.io/
  • Finally change the icon.
  • The Python Package can be pack to .exe and the PATH register can made with .bat that also convertable to .exe.
  • If you learn more about command in .bat file and make experiments with Resource Hacker & self extract ARC in PeaZip & IExpress, you can group both your app project, python package into one .exe file only. It'll auto install what it need on user machine. Although this way more complex and harder, but then you can custom many install experiences included create desktop shorcut, and install UI, and add to app & feature list, and uninstall ability, and portable app, and serial key require, and custom license agreement, and fast window run box, and many more,..... but the important features you get is non virus false positive block, reduce 200MB to many GB when user install many your python graphic applications.
phnghue
  • 1,578
  • 2
  • 10
  • 9