Differences between revisions 9 and 10
Revision 9 as of 2004-10-12 12:05:49
Size: 2904
Editor: www
Comment:
Revision 10 as of 2007-04-13 06:54:42
Size: 2901
Editor: JensDiemer
Comment: path bugfix
Deletions are marked like this. Additions are marked like this.
Line 108: Line 108:
 echo setup (console=["%~dpn0.py"],  echo setup (console=[r"%~dpn0.py"],
Line 125: Line 125:

What is it

I use a short Windows Batch File to "automate" Py2EXE

How it works:

  • Create the Py2EXE SetupFile

  • Compile the Python Script
  • Move the Py2EXE-Results to a Subdirectory
  • Cleans the File-System (delete build and dist)

  • Optional Recompress library.zip with [http://www.7-zip.org/ 7-Zip]

  • Optional Compress *.pyd, *.dll and *.exe Files with [http://upx.sourceforge.net/ UPX]

(About Compression see BetterCompression under 7ZIP and UPX)

Instruction

I use it under Win XP... It should also run under NT and 2000, but i don't know...

You must change the Path to the Apps in the Batch-File!!!

The BatchFile is very flexible, it uses its own file name to locate the Python Script!BR You must rename it to exactly the same name like the script to compile. Example:

  • Name of the Pythonscript to Compile: MyScript.py

  • You must rename the BatchFile to: MyScript.cmd

To change the content of the Py2EXE-Setup-File, you must change the :MakeSetupFile section in the BatchFile

flexible?BR So you can quick use the Batch File with other Python script, yust copy and rename it!

The Batch``File

@echo off


::Set personal Path to the Apps:
set PythonEXE=C:\Python\python.exe
set SevenZipEXE=D:\Tools\7-ZIP\7z.exe
set UpxEXE=D:\Tools\upx\upx.exe


:: Compress=1 - Use CompressFiles
:: Compress=0 - Don't CompressFiles
set Compress=1


if not exist %~dpn0.py          call :FileNotFound %~dpn0.py
if not exist %PythonEXE%        call :FileNotFound %PythonEXE%
if not exist %SevenZipEXE%      call :FileNotFound %SevenZipEXE%
if not exist %UpxEXE%           call :FileNotFound %UpxEXE%


::Write the Py2EXE-Setup File
call :MakeSetupFile >"%~dpn0_EXESetup.py"


::Compile the Python-Script
%PythonEXE% "%~dpn0_EXESetup.py" py2exe
if not "%errorlevel%"=="0" (
        echo Py2EXE Error!
        pause
        goto:eof
)


:: Delete the Py2EXE-Setup File
del "%~dpn0_EXESetup.py"


:: Copy the Py2EXE Results to the SubDirectory and Clean Py2EXE-Results
rd build /s /q
xcopy dist\*.* "%~dpn0_EXE\" /d /y
rd dist /s /q


if "%Compress%"=="1" call:CompressFiles
echo.
echo.
echo Done: "%~dpn0_EXE\"
echo.
pause
goto:eof



:CompressFiles
        %SevenZipEXE% -aoa x "%~dpn0_EXE\library.zip" -o"%~dpn0_EXE\library\"
        del "%~dpn0_EXE\library.zip"

        cd %~dpn0_EXE\library\
        %SevenZipEXE% a -tzip -mx9 "..\library.zip" -r
        cd..
        rd "%~dpn0_EXE\library" /s /q

        cd %~dpn0_EXE\
        %UpxEXE% --best *.*
goto:eof



:MakeSetupFile
        echo.
        echo from distutils.core import setup
        echo import py2exe
        echo.
        echo setup (console=[r"%~dpn0.py"],
        echo    options = {"py2exe": {"packages": ["encodings"]}})
        echo.
goto:eof


:FileNotFound
        echo.
        echo Error, File not found:
        echo [%1]
        echo.
        echo Check Path in %~nx0???
        echo.
        pause
        exit
goto:eof

WinBatch (last edited 2009-04-07 19:06:46 by ChaimKrause)