2017.12.28 うえすぎ

standalone exe from python

1. install python (version 3.6)
   https://www.anaconda.com/download/
   Start "anaconda prompt". cd to python source.

2. install cx_Freeze
   pip install cx_Freeze

3. make setup.py

***setup.py***
from cx_Freeze import setup, Executable
base = None
executables = [Executable("XXXXX.py", base=base)]
packages = ["idna"]
options = {
    'build_exe': {
        'packages':packages,
    },
}

setup(
    name = "test",
    options = options,
    version = "0.5",
    description = 'test',
    executables = executables
)

**************

4. compile
   python setup.py build
     XXXXX.exe is generated in "build\exe.win-amd64-3.6" with a lot of dlls.

   同じバージョンの python がインストールされていればこれだけでも良いはず。


5. install NSIS (portable)
   https://portableapps.com/apps/development/nsis_portable

6. make path (in anaconda prompt)
   set path=%PATH%;c:\tool\NSISPortable\App\NSIS

7. copy the icon file
   ex. copy py.ico to python source code

8. make setup.nis. Change the target name (XXXXX.exe) as your file name.

***setup.nis***
!define cx_FreezeOutputDir 'build\exe.win-amd64-3.6'
!define exe             'XXXXX.exe'
!define icon            'py.ico'
!define compressor      'lzma'  ;one of 'zlib', 'bzip2', 'lzma'
!define onlyOneInstance
!include FileFunc.nsh
!insertmacro GetParameters

; - - - - do not edit below this line, normaly - - - -
!ifdef compressor
    SetCompressor ${compressor}
!else
    SetCompress Off
!endif
Name ${exe}
OutFile ${exe}
SilentInstall silent
!ifdef icon
    Icon ${icon}
!endif

; - - - - Allow only one installer instance - - - - 
!ifdef onlyOneInstance
Function .onInit
 System::Call "kernel32::CreateMutexA(i 0, i 0, t '$(^Name)') i .r0 ?e"
 Pop $0
 StrCmp $0 0 launch
  Abort
 launch:
FunctionEnd
!endif
; - - - - Allow only one installer instance - - - - 

Section
    
    ; Get directory from which the exe was called
    System::Call "kernel32::GetCurrentDirectory(i ${NSIS_MAX_STRLEN}, t .r0)"
    
    ; Unzip into pluginsdir
    InitPluginsDir
    SetOutPath '$PLUGINSDIR'
    File /r '${cx_FreezeOutputDir}\*.*'
    
    ; Set working dir and execute, passing through commandline params
    SetOutPath '$0'
    ${GetParameters} $R0
    ExecWait '"$PLUGINSDIR\${exe}" $R0' $R2
    SetErrorLevel $R2
 
SectionEnd

***************

9. generate standalone version exe.
   makensis setup.nis


Thank you.

ref
http://fanblogs.jp/mountain4101/archive/59/0
http://www.py2exe.org/index.cgi/SingleFileExecutable