[파이썬, Python] py2exe로 실행파일 만들기


Python으로 여러가지 편리한 툴을 만들어 쓰면, 굉장히 편리한 경우가 많죠!

Python 소스 상태로 사용할 경우의 장점은 OS에 무관하게 쓸 수 있다는 장점이 있죠.


하지만, Python이 설치되어 있지 않은 환경에서는 사용할 수 없다는 단점이 있습니다.

그리고, Python 소스에서 특정한 라이브러리를 include하였을 경우에는, 사용하려는 환경의 Python에도 동일한 라이브러리가 설치되어 있어만 한다는 단점이 있습니다.


이러한 단점을 보완하기 위해서, windows 환경을 사용하는 사람들끼리는 Python 소스를 exe 파일로 만들어서 제공하는 방법이 있습니다.

Python 소스를 exe로 만들 때, 가장 많이 사용하는 것이, py2exe입니다.



[py2exe 다운로드 및 설치]


우선, 아래 Link로부터 설치파일을 받아서 py2exe 설치를 합니다.





PC에 Python2.7 이 설치되어 있다면, "py2exe-0.6.9.win32-py2.7.exe"을 받습니다.

(만약 PC에 Python2.5가 설치되어 있다면, "py2exe-0.6.9.win32-py2.5.exe"를 받으면 됩니다.)




다운로드 받은 설치 실행 파일을 실행시켜서, "다음 (Next)"만 클릭하면 손쉽게 설치가 됩니다.





[py2exe 사용법]


1. setup.py 생성


다음과 같은 setup.py 를 하나 만듭니다. 아래 예제에서 exe로 만들 파일이름은 "test.py"입니다.


from distutils.core import setup

import py2exe, sys, os


includes  = [

 "encodings",

 "encodings.utf_8",

]

 

options = {

 "bundle_files": 1,                 # create singlefile exe

 "compressed": 1,                 # compress the library archive

 "optimize": 2,                 # do optimize

 "includes": includes,

}

 

setup(

 options = {"py2exe" : options},

 console = [{'script': "test.py"}],

 zipfile = None,

)



2. setup.py 실행


아래와 같이 setup.py를 실행시키면, exe파일이 dist 폴더 아래에 생성됩니다.


python setup.py py2exe



3. 기타


위의 예제 setup.py를 실행하면, 아래와 같은 경고문구가 발생합니다. 


*** binary dependencies ***

Your executable(s) also depend on these dlls which are not included,

you may or may not need to distribute them.


Make sure you have the license if you distribute any of them, and

make sure you don't distribute files belonging to the operating system.


   WSOCK32.dll - C:\Windows\system32\WSOCK32.dll

   USER32.dll - C:\Windows\system32\USER32.dll

   ADVAPI32.dll - C:\Windows\system32\ADVAPI32.dll

   SHELL32.dll - C:\Windows\system32\SHELL32.dll

   KERNEL32.dll - C:\Windows\system32\KERNEL32.dll



4. dll_excludes 옵션


아래 예제와 같이, dll_excludes 옵션을 사용하면 *.dll 파일이 포함되지 않도록 할 수 있습니다.


from distutils.core import setup

import py2exe, sys, os


includes  = [

"encodings",

"encodings.utf_8",

]

 

options = {

 "bundle_files": 1,                 # create singlefile exe

 "compressed"  : 1,                 # compress the library archive

 "optimize"    : 2,                 # do optimize

 "dll_excludes": ["WSOCK32.dll", "USER32.dll", "ADVAPI32.dll", "SHELL32.dll", "KERNEL32.dll"],

 "includes": includes,

}

 

setup(

 options = {"py2exe" : options},

 console = [{'script': "test.py"}],

 zipfile = None,

)


5. 예제 파일 첨부


setup.py


6. 기타 옵션


Using "bundle_files" and "zipfile"

An easier (and better) way to create single-file executables is to set bundle_files to 1 or 2, and to set zipfile to None. This approach does not require extracting files to a temporary location, which provides much faster program startup.


Valid values for bundle_files are:

3 (default)

don't bundle

2

bundle everything but the Python interpreter

1

bundle everything, including the Python interpreter


If zipfile is set to None, the files will be bundle within the executable instead of library.zipNote: you will still be required to include the MSVC runtime DLL with your application, which should be located in the dist directory along side the executable (named MSVCRXX.dll, where XX = revision number).


Here is a sample setup.py:

Toggle line numbers
   1 from distutils.core import setup
   2 import py2exe, sys, os
   3 
   4 sys.argv.append('py2exe')
   5 
   6 setup(
   7     options = {'py2exe': {'bundle_files': 1}},
   8     windows = [{'script': "single.py"}],
   9     zipfile = None,
  10 )

(출처: http://www.py2exe.org/index.cgi/SingleFileExecutable) 

반응형

+ Recent posts