running .exe file compiled by Pyinstaller cannot suppress the tshark console window

In my Python project, there is a main.py which is the entrance of the application and uses Tkinter to design UI. There are some other .py files which will be called and run from main.py. One of the .py files is process_pcap.py which imports Pyshark API to call tshark to process a .pcap log. For convenience to distribute the application, I use Pyinstaller to convert the main.py to main.exe.

When I run main.exe to open the application UI and then select to execute process_pcap (via calling class PCAP defined in the process_pcap.py), it pops up a ‘tshark’ console window. This is not what I want. I want to prevent the tshark console window from popping up.

But when I directly run python main.py to execute process_pcap, the tshark console window will not pop up.

I have tried some ways to suppress the tshark console window, but none of them works.

  1. I tried the commands below to convert main.py to main.exe. But it could not suppress the tshark console window.
pyinstaller --onefile --windowed main.py
pyinstaller --onefile --noconsole main.py
  1. I also tried modifying the subprocess.Popen and subprocess.check_output in the process_pcap.py, but it couldn’t suppress the tshark console window either.
import pyshark
import subprocess
import sys

class PCAP:
    def __init__(self, pcap_file, redirector_stdout):
        self.pcap_file = pcap_file
        self.redirector_stdout = redirector_stdout
        self.suppress_console_window()

    def suppress_console_window(self):
        if sys.platform.startswith('win'):
            # Save the original subprocess.Popen and subprocess.check_output
            original_popen = subprocess.Popen
            original_check_output = subprocess.check_output

            # Define a new Popen that suppresses the console window
            def new_popen(*args, **kwargs):
                if 'startupinfo' not in kwargs:
                    startupinfo = subprocess.STARTUPINFO()
                    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
                    kwargs['startupinfo'] = startupinfo

                # Add CREATE_NO_WINDOW as well for extra suppression
                if 'creationflags' not in kwargs:
                    kwargs['creationflags'] = subprocess.CREATE_NO_WINDOW

                kwargs['shell'] = True  # Ensure shell execution
                return original_popen(*args, **kwargs)

            # Define a new check_output that suppresses the console window
            def new_check_output(*args, **kwargs):
                if 'startupinfo' not in kwargs:
                    startupinfo = subprocess.STARTUPINFO()
                    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
                    kwargs['startupinfo'] = startupinfo

                if 'creationflags' not in kwargs:
                    kwargs['creationflags'] = subprocess.CREATE_NO_WINDOW

                kwargs['shell'] = True
                return original_check_output(*args, **kwargs)

            # Override subprocess.Popen and subprocess.check_output with the new versions
            subprocess.Popen = new_popen
            subprocess.check_output = new_check_output

    def execute_code(self):
        # Process the pcap file using Pyshark without showing the tshark console
        cap = pyshark.FileCapture(self.pcap_file, display_filter='sip')

        for packet in cap:
            self.redirector_stdout.write(f"packet = {packet}n")
            if 'sip' in packet:
                sip_layer = packet['sip']
                self.redirector_stdout.write(f"sip_layer = {sip_layer}n")

if __name__ == "__main__":
    # Example usage when running this script directly
    pcap = PCAP('sample.pcap', sys.stdout) 
    pcap.execute_code()

Does anyone have a solution to suppress the tshark console window for my case above? Thanks.

New contributor

Jinshi Qiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật