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

2016. 9. 12. 11:33·IT기술 관련/파이썬_루비 등 언어
반응형

출처:http://thecoollife.tistory.com/704


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


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

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


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

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


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

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



[py2exe 다운로드 및 설치]


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


http://www.py2exe.org/




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.zip. Note: 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) 

반응형
저작자표시 (새창열림)

'IT기술 관련 > 파이썬_루비 등 언어' 카테고리의 다른 글

Python pexpect 모듈을 윈도우에서 사용하기  (0) 2016.09.12
[파이썬, Python] 하위 폴더를 포함한 파일 리스트 출력하기 예제 2.  (0) 2016.09.12
python - Reading Excel sheet with xlrd (xlrd를 이용한 파이썬 엑셀 파일 읽기)  (0) 2016.09.12
python - pxssh를 이용한 ssh connect & send command  (0) 2016.09.12
구글 트렌드 파이썬 api  (1) 2016.04.25
'IT기술 관련/파이썬_루비 등 언어' 카테고리의 다른 글
  • Python pexpect 모듈을 윈도우에서 사용하기
  • [파이썬, Python] 하위 폴더를 포함한 파일 리스트 출력하기 예제 2.
  • python - Reading Excel sheet with xlrd (xlrd를 이용한 파이썬 엑셀 파일 읽기)
  • python - pxssh를 이용한 ssh connect & send command
호레
호레
창업 / IT / 육아 / 일상 / 여행
    반응형
  • 호레
    Unique Life
    호레
  • 전체
    오늘
    어제
    • 분류 전체보기
      • 법률
        • 기본
        • 개인정보보호법
        • 정보통신망법
        • 전자금융거래법
        • 전자금융감독규정
        • 신용정보법
        • 온라인투자연계금융업법
      • 창업
        • 외식업 관련
        • 임대업 관련
        • 유통업 관련
        • 세무 관련
        • 마케팅 관련
        • 기타 지식
        • 트렌드
        • Youtube
      • IT기술 관련
        • 모바일
        • 윈도우
        • 리눅스
        • MAC OS
        • 네트워크
        • 빅데이터 관련
        • A.I 인공지능
        • 파이썬_루비 등 언어
        • 쿠버네티스
        • 기타 기술
      • 퍼블릭 클라우드 관련
        • Azure
        • GCP
        • AWS
      • 정보보안 관련
        • QRadar
        • Splunk
        • System
        • Web
      • 기타
        • 세상 모든 정보
        • 서적
      • 게임 관련
        • 유니티
      • 부동산
      • 맛집 찾기
        • 강남역
        • 양재역
        • 판교역
        • ★★★★★
        • ★★★★
        • ★★★
        • ★★
        • ★
      • 결혼_육아 생활
        • 리얼후기
        • 일상
        • 육아
        • 사랑
        • Food
      • 영어
        • 스피킹
        • 문법
        • 팝송
        • 영화
      • K-컨텐츠
        • 드라마
        • 영화
        • 예능
      • 독서
      • 프로젝트 관련 조사
        • 시스템 구축
        • 로그 관련
        • 웹
        • APT
        • 모의 해킹
        • DB
        • 허니팟
        • 수리카타
        • 알고리즘
        • FDS
      • 기업별 구내 식당 평가
        • 한국관광공사
        • KT telecop
        • KT M&S
        • KT powertel
        • KT cs 연수원
        • 진에어
      • 대학 생활
        • 위드윈연구소
        • 진로 고민
        • 채용정보
        • 자동차
        • 주식
        • 악성코드
        • 게임 보안
      • 쉐어하우스
  • 블로그 메뉴

    • 홈
    • 게임 관련
    • IT 기술 관련
    • 태그
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    대통령
    맛집
    복리후생
    유니티
    수제버거맛집
    판교
    판교역
    쥬쥬랜드
    점심
    마케팅
    이재곧죽습니다
    보안가이드
    런치
    수제버거
    돈까스
    수제버거존맛
    상호관세
    판교맛집
    무역전쟁
    AWS
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
호레
[파이썬, Python] py2exe로 실행파일 만들기
상단으로

티스토리툴바