python pyinstaller 에서 webdriver 사용시에 window console 창이 뜨지 않게 하는 방법
페이지 정보
작성자 관리자 (112.♡.173.204) 작성일 21-04-28 10:50 조회 4,171 댓글 0본문
## pyinstaller
python 파일을 윈도 실행파일로 만드는 방법 중에는 pyinstaller 를 이용하는 방법이 있다.
기본적인 사용방법과 옵션은 다음과 같다.
```
pyinstaller -w -F test.py
```
-w, --windowed, --noconsole : window cosole 창을 띄우지 않는다. 검은색 cmd 창
-F, --onefile : 하나의 실행파일로 만든다.
- 참고 : https://pyinstaller.readthedocs.io/en/stable/usage.html#options
## 문제점
pyinstaller 의 -w 옵션으로 만든 test.exe 파일 자체의 콘솔창은 뜨지 않는다.
그런데, webdriver 인 chromedriver.exe 의 콘솔창이 떠 버린다.
반대로, -w 옵션 없이 실행파일을 만들면, chromedriver.exe 의 콘솔창은 안뜨는데, test.exe 의 콘솔창이 떠 버린다.
## 해결방법
webdriver 의 service.py 파일의 내용을 약간 수정하여, 해결이 가능하다.
service.py 파일의 위치는 아래에 있으며, python 의 설치 위치에 따라 달라질 수 있다.
C:\Python39\Lib\site-packages\selenium\webdriver\common\service.py
service.py 파일에서 def start(self): 함수 부분의 Popen 에 creationflags=0x08000000 를 추가해 준다.
옵션을 주어 분기하는 방법도 있으나, 여기서는 그냥 일괄 처리한다. 분기 방법은 아래 참고를 참조하면 된다.
## service.py
```
try:
cmd = [self.path]
cmd.extend(self.command_line_args())
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE)
```
를
```
try:
cmd = [self.path]
cmd.extend(self.command_line_args())
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE,
creationflags=0x08000000)
```
로 변경한다.
그리고, 실행파일을 다시 빌드하여 실행해 보면, 콘솔창이 뜨지 않는다.
## 참고
https://m.blog.naver.com/kibani/221921810439
https://sijoo.tistory.com/409
https://stackoverflow.com/questions/48654427/hide-command-prompt-in-selenium-chromedriver
python 파일을 윈도 실행파일로 만드는 방법 중에는 pyinstaller 를 이용하는 방법이 있다.
기본적인 사용방법과 옵션은 다음과 같다.
```
pyinstaller -w -F test.py
```
-w, --windowed, --noconsole : window cosole 창을 띄우지 않는다. 검은색 cmd 창
-F, --onefile : 하나의 실행파일로 만든다.
- 참고 : https://pyinstaller.readthedocs.io/en/stable/usage.html#options
## 문제점
pyinstaller 의 -w 옵션으로 만든 test.exe 파일 자체의 콘솔창은 뜨지 않는다.
그런데, webdriver 인 chromedriver.exe 의 콘솔창이 떠 버린다.
반대로, -w 옵션 없이 실행파일을 만들면, chromedriver.exe 의 콘솔창은 안뜨는데, test.exe 의 콘솔창이 떠 버린다.
## 해결방법
webdriver 의 service.py 파일의 내용을 약간 수정하여, 해결이 가능하다.
service.py 파일의 위치는 아래에 있으며, python 의 설치 위치에 따라 달라질 수 있다.
C:\Python39\Lib\site-packages\selenium\webdriver\common\service.py
service.py 파일에서 def start(self): 함수 부분의 Popen 에 creationflags=0x08000000 를 추가해 준다.
옵션을 주어 분기하는 방법도 있으나, 여기서는 그냥 일괄 처리한다. 분기 방법은 아래 참고를 참조하면 된다.
## service.py
```
try:
cmd = [self.path]
cmd.extend(self.command_line_args())
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE)
```
를
```
try:
cmd = [self.path]
cmd.extend(self.command_line_args())
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE,
creationflags=0x08000000)
```
로 변경한다.
그리고, 실행파일을 다시 빌드하여 실행해 보면, 콘솔창이 뜨지 않는다.
## 참고
https://m.blog.naver.com/kibani/221921810439
https://sijoo.tistory.com/409
https://stackoverflow.com/questions/48654427/hide-command-prompt-in-selenium-chromedriver
추천0
댓글목록 0
등록된 댓글이 없습니다.