Using Tshark in compiled python exe
Question:
I have a working python script, which uses and imports pyshark, and therefore tshark.
As long as I run the code via pycharm, everything is fine. As soon as I use pyinstaller to compile, I get this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter__init__.py", line 1921, in __call__
File "main.py", line 458, in get_pdu_ids
File "pysharkcapturecapture.py", line 232, in _packets_from_tshark_sync
File "asynciobase_events.py", line 646, in run_until_complete
File "pysharkcapturecapture.py", line 396, in _get_tshark_process
File "pysharkcapturecapture.py", line 373, in _get_tshark_path
File "pysharktsharktshark.py", line 30, in get_process_path
File "configparser.py", line 782, in get
File "configparser.py", line 1153, in _unify_values
configparser.NoSectionError: No section: 'tshark'
I tried to use this fix, which should as well be merged into pyshark by now, but it didn`t change anything: https://github.com/KimiNewt/pyshark/issues/245
Of course everything is up-to-date.
Note: Every other imported module works flawlessly, e.g. scapy.
Thanks in advance!
Answers:
I found the solution, posting it here to help if others get the same error:
I had to hard-code the following, as the compiled program didn`t get the variable correct (no idea why):
File: venvLibsite-packagespysharkcapturecapture.py
def _get_tshark_path(self):
return get_process_path(self.tshark_path)
to
def _get_tshark_path(self):
return r"C:/Program Files/Wireshark/tshark.exe"
It is a dirty solution, not solving but bypassing the problem, but it works and I wanted to share it nonetheless.
Try:
pyinstaller –collect-all pyshark -F Main.py
This should resolve the issue
Although this question has been answered, I have had a similar issue and I would like to share my solution.
It seems that pyshark will load any file named ‘config.ini’ in the current directory as if it were the tshark configuration file. From the get_process_path documentation:
"""Finds the path of the tshark executable.
If the user has provided a path
or specified a location in config.ini it will be used. Otherwise default
locations will be searched.
:param tshark_path: Path of the tshark binary
:raises TSharkNotFoundException in case TShark is not found in any
location.
"""
Indeed, if we take a look at config.py in the pyshark module that is called to find the tshark directory we can find :
fp_config_path = Path.cwd() / 'config.ini' # get config from the current
directory
pyshark_config_path = Path(pyshark.__file__).parent / 'config.ini'
def get_config():
if fp_config_path.exists():
config_path = fp_config_path
elif pyshark_config_path.exists():
config_path = pyshark_config_path
else:
return None
The solution is to either rename your configuration file, move it to a subdirectory, or include the tshark and dumpcap sections in your config file like this:
[tshark]
# Specify the path to the tshark executable.
# If the configured path does not exist, these locations will be searched:
# (Linux): /usr/bin/tshark
# (Linux): /usr/sbin/tshark
# (Linux): /usr/lib/tshark/tshark
# (Linux): /usr/local/bin/tshark
# (Windows): %ProgramFiles%Wiresharktshark.exe
# (Windows): %ProgramFiles(x86)%Wiresharktshark.exe
tshark_path = C:Program FilesWiresharktshark.exe
[dumpcap]
dumpcap_path = C:Program FilesWiresharkdumpcap.exe
I have a working python script, which uses and imports pyshark, and therefore tshark.
As long as I run the code via pycharm, everything is fine. As soon as I use pyinstaller to compile, I get this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter__init__.py", line 1921, in __call__
File "main.py", line 458, in get_pdu_ids
File "pysharkcapturecapture.py", line 232, in _packets_from_tshark_sync
File "asynciobase_events.py", line 646, in run_until_complete
File "pysharkcapturecapture.py", line 396, in _get_tshark_process
File "pysharkcapturecapture.py", line 373, in _get_tshark_path
File "pysharktsharktshark.py", line 30, in get_process_path
File "configparser.py", line 782, in get
File "configparser.py", line 1153, in _unify_values
configparser.NoSectionError: No section: 'tshark'
I tried to use this fix, which should as well be merged into pyshark by now, but it didn`t change anything: https://github.com/KimiNewt/pyshark/issues/245
Of course everything is up-to-date.
Note: Every other imported module works flawlessly, e.g. scapy.
Thanks in advance!
I found the solution, posting it here to help if others get the same error:
I had to hard-code the following, as the compiled program didn`t get the variable correct (no idea why):
File: venvLibsite-packagespysharkcapturecapture.py
def _get_tshark_path(self):
return get_process_path(self.tshark_path)
to
def _get_tshark_path(self):
return r"C:/Program Files/Wireshark/tshark.exe"
It is a dirty solution, not solving but bypassing the problem, but it works and I wanted to share it nonetheless.
Try:
pyinstaller –collect-all pyshark -F Main.py
This should resolve the issue
Although this question has been answered, I have had a similar issue and I would like to share my solution.
It seems that pyshark will load any file named ‘config.ini’ in the current directory as if it were the tshark configuration file. From the get_process_path documentation:
"""Finds the path of the tshark executable.
If the user has provided a path
or specified a location in config.ini it will be used. Otherwise default
locations will be searched.
:param tshark_path: Path of the tshark binary
:raises TSharkNotFoundException in case TShark is not found in any
location.
"""
Indeed, if we take a look at config.py in the pyshark module that is called to find the tshark directory we can find :
fp_config_path = Path.cwd() / 'config.ini' # get config from the current
directory
pyshark_config_path = Path(pyshark.__file__).parent / 'config.ini'
def get_config():
if fp_config_path.exists():
config_path = fp_config_path
elif pyshark_config_path.exists():
config_path = pyshark_config_path
else:
return None
The solution is to either rename your configuration file, move it to a subdirectory, or include the tshark and dumpcap sections in your config file like this:
[tshark]
# Specify the path to the tshark executable.
# If the configured path does not exist, these locations will be searched:
# (Linux): /usr/bin/tshark
# (Linux): /usr/sbin/tshark
# (Linux): /usr/lib/tshark/tshark
# (Linux): /usr/local/bin/tshark
# (Windows): %ProgramFiles%Wiresharktshark.exe
# (Windows): %ProgramFiles(x86)%Wiresharktshark.exe
tshark_path = C:Program FilesWiresharktshark.exe
[dumpcap]
dumpcap_path = C:Program FilesWiresharkdumpcap.exe