本文介绍了vscode 导入错误:从 scapy.all 导入 IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

vscode 说在 scapy.all 中找不到 IP

vscode said can't find IP in scapy.all

但是从终端,我可以导入它:

but from terminal, i can import it:

有人能告诉我为什么吗?

could somebody tell my why?

推荐答案

我在 VS Code 中遇到了与我的 Scapy 代码完全相同的问题.我认为这与 pylint 的工作方式有关.

I get exactly the same issue with my Scapy code in VS Code. I think it's to do with the way pylint is working.

当你from scapy.all import IP时,Python会加载scapy/all.py,其中包括from scapy.layers.all import *.scapy/layers/all.py 包含以下代码:

When you from scapy.all import IP, Python loads scapy/all.py, which includes the line from scapy.layers.all import *. scapy/layers/all.py includes this code:

for _l in conf.load_layers:
    log_loading.debug("Loading layer %s" % _l)
    try:
        load_layer(_l, globals_dict=globals(), symb_list=__all__)
    except Exception as e:
        log.warning("can't import layer %s: %s", _l, e)

conf.load_layersscapy/config.py 中结束:

load_layers = ['bluetooth', 'bluetooth4LE', 'dhcp', 'dhcp6', 'dns',
               'dot11', 'dot15d4', 'eap', 'gprs', 'hsrp', 'inet',
               'inet6', 'ipsec', 'ir', 'isakmp', 'l2', 'l2tp',
               'llmnr', 'lltd', 'mgcp', 'mobileip', 'netbios',
               'netflow', 'ntp', 'ppp', 'pptp', 'radius', 'rip',
               'rtp', 'sctp', 'sixlowpan', 'skinny', 'smb', 'snmp',
               'tftp', 'vrrp', 'vxlan', 'x509', 'zigbee']

我怀疑 pylint 没有正确遵循这些导入.

I suspect that pylint doesn't follow those imports correctly.

我已经尝试了相关 GitHub 问题中建议的解决方法,但他们没有似乎没有为 Scapy 解决任何问题.Pylint 最终为 Numpy 中的问题添加了特定解决方法 - 没有人为Scapy.

I've tried the workarounds suggested in the relevant GitHub issue, but they don't seem to fix anything for Scapy. Pylint eventually added specific workarounds for the issues in Numpy - and no-one has done that for Scapy.

您可以通过直接从 Python 文件顶部的相关层导入 IP 类来解决这些问题:

You can work around these issues by directly importing the IP class from the relevant layer at the top of your Python file:

from scapy.layers.inet import IP, UDP, TCP, ICMP

等等!不再有关于这些进口的 pylint 投诉.

Et voila! No more pylint complaints about those imports.

这篇关于vscode 导入错误:从 scapy.all 导入 IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 00:36