ML-개발환경/OS

[ubuntu18.04] pyinstaller 사용법 및 문제해결

dohyeon2 2022. 8. 29. 22:10

오늘은 pyinstaller를 사용해 여러개의 모듈이 묶인 python code를 deploy하는 방법을 정리하려 한다.

리눅스는 윈도우10과 달리 file_name.spec 라는 파일이 생성되지 않는다. 따라서 하나하나 라이브러리 경로를 입력해줄 필요가 없어서 윈도우에 비해 간편하다. 


0. python code가 동작하는 가상환경 활성화 

conda activate 가상환경이름

 

1. pyinstaller 설치 

pip install pyinstaller

 

2. pyinstaller로 실행파일로 변환

pyinstaller --distpath /scratch/dohyeon main.py

--distpath : 실행파일이 저장될 디렉토리 주소

 

3. 문제해결

 

에러 1 -  Assertionerror: /scratch/dohyeon/main/distutils/core.pyc

위 에러는 setuptools==60.0.0 버전 이상부터 발생하는 에러이다. 

아래 명령어로 다운그레이드 시키면 해결된다.

pip install --upgrade setuptools==59.8.0

reference: https://stackoverflow.com/questions/71027006/assertionerror-inside-of-ensure-local-distutils-when-building-a-pyinstaller-exe

 

AssertionError inside of ensure_local_distutils when building a PyInstaller exe using setuptools/distutils

I am trying to convert some Python code into an .exe with PyInstaller. My code uses distutils, which has already caused me some head scratching in the past as it seems to duplicate setuptools

stackoverflow.com

 

에러 2 - pyinstaller error : ModuleNotFoundError: No module named 'mmcv._ext'

위 에러는 mmdetection code를 실행할 때 가상환경이 아닌 pyinstaller로 묶어주면 발생하는 에러이다.

위 에러는 --hidden-import 옵션을 추가하면 해결된다.

아래 명령어로 실행파일을 생성한다. 

pyinstaller --hidden-import=mmcv._ext --hidden-import torchvision --distpath /scratch/dohyeon main.py

reference: https://bytemeta.vip/repo/open-mmlab/mmsegmentation/issues/854?page=2 

 

pyinstaller error : ModuleNotFoundError: No module named 'mmcv._ext' - bytemeta

pyinstaller error : ModuleNotFoundError: No module named 'mmcv._ext'

bytemeta.vip

 

4. 실행파일 생성하기 

pyinstaller --hidden-import=mmcv._ext --hidden-import torchvision --distpath /scratch/dohyeon main.py

 

 

5. 생성된 python file 이름 폴더로 들어가서 실행파일 동작해보기

나의 경우 디렉토리 경로를 /scratch/dohyeon 이라는 곳에 설정해놓았기 때문에 

해당 디렉토리에 main 이라는 디렉토리가 생성되었고 그 안에 아래 사진과 같이 많은 파일들이 생성되었다. 

이중 main 이라는 파일을 아래 명령어로 실행해보면 문제없이 동작하는 것을 확인할 수 있다. 

./main