pyinstaller 로 exe파일 만들때 ui 파일 포함시키기 > IT 기술백서

IT 기술백서

직접 알아내거나 검색하기 귀찮아서 모아 둔 것

python | pyinstaller 로 exe파일 만들때 ui 파일 포함시키기

본문

qt 디자이너로 만든 ui파일을 이용하여 프로그램을 만들었을 경우 pyinstaller 로 빌드하면 ui파일이 포함되지 않는다.

해당 파일을 exe 실행파일이 있는곳에 복사해주면 되지만 폼소스가 노출되는것이 찝찝하다.

ui 파일도 exe파일에 포함시켜주는 방법은 아래와 같다.

 

먼저 파일과 폴더의 구조가 아래와 같고 Window Form 은 mainwindow, account 두개가 있다고 가정한다.

[code]

util.py

ui/

    account.ui

    mainwindow.ui

    

account.ui

mainwindow.py

[/code]




util.py

[code]

# 리소스에 대한 절대경로 가져오기 (개발환경과 pyinstaller 에서 작동한다)

def resource_path(relative_path):

    base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))

    return os.path.join(base_path, relative_path)

[/code]



mainwindow.py

[code]

from util import resource_path


form = resource_path("ui/mainwindow.ui")

form_class = uic.loadUiType(form)[0]


class MainWindow(QMainWindow, form_class):

    def __init__(self):

        super().__init__()

        self.setupUi(self)

[/code]


account.py

[code]

from util import resource_path


form = resource_path("ui/account.ui")

form_class = uic.loadUiType(form)[0]


class Account(QMainWindow, form_class):

    def __init__(self):

        super().__init__()

        self.setupUi(self)

[/code]


먼저 mainwindow.py 를 빌드한다. 빌드옵션은 알아서들...

[code]

pyinstaller --clean --onefile --noconsole --key=myprogramkey -n myprogram mainwindow.py

[/code]

--clean: 기존의 찌꺼기를 모두 삭제한다.

--onefile: 파일 한개로 만든다

--noconsole: 실행시 콘솔창을 띄우지 않는다

--key: tinyaes 암호화를 위한 키

-n: 최종 생성될 파일 이름


빌드 하고 나면 spec 파일이 생겨난다.  이 파일을 열고 아래와 같이 datas 부분에 ui를 추가한다.

[code]

a = Analysis(['mainwindow.py'],

            ....

            datas=[('ui/account.ui', './ui'),

                ('ui/mainwindow.ui', './ui')],

            ....

[/code]


저장하고 난뒤에 이 스펙파일을 이용하여 빌드한다.

[code]

pyinstaller mainwindow.spec

[/code]


dist/myprogram.exe 를 실행해보자

 

 

댓글 0개

등록된 댓글이 없습니다.

Menu