C:\Users\HUAWEI\AppData\Local\Programs\Python\Python312\python.exe D:\pc01\pc.py Traceback (most recent call last): File "D:\pc01\pc.py", line 4, in <module> capture = pyshark.LiveCapture( ^^^^^^^^^^^^^^^^^^^^ File "C:\Users\HUAWEI\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyshark\capture\live_capture.py", line 58, in __init__ all_interfaces = get_tshark_interfaces(tshark_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\HUAWEI\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyshark\tshark\tshark.py", line 112, in get_tshark_interfaces parameters = [get_process_path(tshark_path), "-D"] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\HUAWEI\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyshark\tshark\tshark.py", line 70, in get_process_path raise TSharkNotFoundException( pyshark.tshark.tshark.TSharkNotFoundException: TShark not found. Try adding its location to the configuration file. Searched these paths: ['C:\\Program Files\\Wireshark\\tshark.exe', 'C:\\Program Files (x86)\\Wireshark\\tshark.exe', 'C:\\Program Files\\Wireshark\\tshark.exe'] 进程已结束,退出代码为 1
时间: 2025-07-07 14:19:28 浏览: 13
<think>根据错误信息,程序在尝试调用tshark时没有找到可执行文件。pyshark依赖于Wireshark的tshark工具,因此需要确保tshark已安装并且其路径被正确配置。
解决方案:
1. 安装Wireshark(包含tshark),下载地址:https://www.wireshark.org/download.html
2. 安装时注意勾选"Install TShark"(默认会安装)。
3. 安装完成后,找到tshark的安装路径(例如:C:\Program Files\Wireshark\tshark.exe)。
4. 在Python代码中设置tshark的路径。
另外,也可以将Wireshark的安装目录添加到系统的环境变量PATH中,这样pyshark就能自动找到tshark。
以下代码演示如何在程序中设置tshark路径:</think>### 解决 "TShark not found" 错误
这个错误表明 pyshark 无法找到 Wireshark 的 tshark 可执行文件。pyshark 依赖于 tshark 进行底层数据包处理,因此必须正确配置。以下是完整的解决方案:
#### 解决方案 1:安装 Wireshark(推荐)
```bash
# Windows 用户:
1. 下载 Wireshark 安装包:https://www.wireshark.org/download.html
2. 安装时勾选以下选项:
☑ Install Wireshark
☑ Install TShark (命令行工具)
☑ Add Wireshark to the system PATH
# macOS 用户:
brew install wireshark
# Ubuntu/Debian 用户:
sudo apt update
sudo apt install tshark wireshark
```
#### 解决方案 2:手动指定 tshark 路径(如果已安装)
```python
import pyshark
import os
# Windows 默认路径(根据实际安装位置调整)
tshark_path = r'C:\Program Files\Wireshark\tshark.exe'
# macOS/Linux 默认路径
# tshark_path = '/usr/bin/tshark'
if os.path.exists(tshark_path):
pyshark.tshark.tshark_path = tshark_path
print(f"TShark path set to: {tshark_path}")
else:
print(f"TShark not found at {tshark_path}. Please install Wireshark.")
# 测试配置
try:
capture = pyshark.LiveCapture(interface='eth0')
print("TShark configuration successful!")
except Exception as e:
print(f"Configuration failed: {str(e)}")
```
#### 解决方案 3:Linux/macOS 权限修复
```bash
# 修复 dumpcap 权限(数据包捕获需要)
sudo chgrp $(id -g) /usr/bin/dumpcap
sudo chmod 750 /usr/bin/dumpcap
```
#### 解决方案 4:验证安装的完整代码
```python
import pyshark
import subprocess
def check_tshark_installation():
"""检查系统是否安装了 TShark"""
try:
# 尝试运行 tshark --version
result = subprocess.run(
['tshark', '--version'],
capture_output=True,
text=True,
check=True
)
print("TShark is installed:")
print(result.stdout.split('\n')[0])
return True
except (FileNotFoundError, subprocess.CalledProcessError):
print("TShark not found in system PATH.")
return False
def find_tshark_path():
"""在常见路径中搜索 tshark"""
common_paths = [
# Windows 路径
r'C:\Program Files\Wireshark\tshark.exe',
r'C:\Program Files (x86)\Wireshark\tshark.exe',
# Linux/macOS 路径
'/usr/bin/tshark',
'/usr/local/bin/tshark',
'/opt/wireshark/bin/tshark'
]
for path in common_paths:
if os.path.exists(path):
print(f"Found TShark at: {path}")
return path
print("TShark not found in common locations.")
return None
# 主程序
if not check_tshark_installation():
tshark_path = find_tshark_path()
if tshark_path:
pyshark.tshark.tshark_path = tshark_path
print("TShark path manually configured.")
else:
print("Please install Wireshark from https://www.wireshark.org/download.html")
exit(1)
# 测试实时捕获
try:
interfaces = pyshark.get_all_interfaces()
print(f"Available interfaces: {interfaces}")
if interfaces:
capture = pyshark.LiveCapture(interface=interfaces[0])
print("Starting packet capture... Press Ctrl+C after 5 seconds")
capture.sniff(timeout=5)
print(f"Captured {len(capture)} packets successfully!")
except Exception as e:
print(f"Capture test failed: {str(e)}")
```
### 错误原因详解
1. **依赖缺失**:pyshark 是 TShark 的 Python 封装,必须安装 Wireshark
2. **路径问题**:
- Windows:默认安装在 `C:\Program Files\Wireshark\`
- macOS:通过 Homebrew 安装在 `/usr/local/bin/`
- Linux:通过 apt/yum 安装在 `/usr/bin/`
3. **权限问题**:Linux/macOS 需要 dumpcap 权限才能捕获数据包
### 验证安装成功
运行以下命令检查 TShark 是否可用:
```bash
tshark --version
# 应输出类似:TShark (Wireshark) 4.0.3
```
阅读全文
相关推荐



















