Scapy.all import * does not work

So, I wrote a little script in Ubuntu for scapy.

#!/usr/bin/env python import sys #from scapy.all import * try import scapy except ImportError: del scapy from scapy import all as scapy i= IP() t= TCP() i.dst='192.168.56.100' t.dport=22 pakket=i/t answered,unanswered=sr(pakket) answered.nsummary() 

I wrote try because of a different topic here (tried this as a solution). My current output with this code is as follows

 Traceback (most recent call last): File "./scapy.py", line 5, in <module> import scapy File "/home/nicholas/scapy.py", line 9, in <module> i=IP() NameError: name 'IP' is not defined 

when you try using scapy.all to import * using "try".

 Traceback (most recent call last): File "./scapy.py", line 3, in <module> from scapy.all import * File "/home/nicholas/scapy.py", line 3, in <module> from scapy.all import * ImportError: No module named all 

I tried various import methods found on Google, but it still does not work. Can someone tell me what I'm doing wrong? (not against the indentation of this post)

+8
source share
4 answers

From a look at the source of scapy, the scapy package does not display in import or define __all__ in __init__ . As a result, you need to explicitly import scapy.all (or from scapy import all ) before you can extract anything else from scapy.all import , since it will not be in sys.modules . Please note that this only needs to happen once in your program stream, because after the interpreter imports the module, it will be available for all the code that runs from now on, regardless of where it is located. Take a look at Python docs on modules and how import , and in particular package import, works for more details.

Edit: I think I see the problem now, I just noticed the wrong part of the stack trace. Quite exactly what you mean here is a name clash. Your file is named scapy.py , so when you import scapy from the context of this file, you are actually importing the file itself as a module. Since your file does not have a submodule named all (it cannot, because it is not a package), you get the import error that you see. Try switching your file name to something that does not contradict any packages or modules that you want to import inside, and see if this improves.

By the way, pay attention to the stack trace that your import actually calls your single file recursively. This should be the key to ensure that something is gone during the import process.

+3
source

Pretty old post. For those who are still looking, the correct import with current versions will be:

from scapy.all import *

+2
source

I saw this when I had scapy.py in the current directory. scapy.all import * first looks in the current directory.

0
source

I had a similar problem on OSX, I installed the pip install scapy and then tried to run my test file scapy.py . The error I received was:

 python scapy.py Traceback (most recent call last): File "scapy.py", line 1, in <module> from scapy.all import * File "/Users/**/Desktop/scapy-test/scapy.py", line 1, in <module> from scapy.all import * ModuleNotFoundError: No module named 'scapy.all'; 'scapy' is not a package 

In my case, it was the file name that caused the problem; it cannot be called scapy.py . I changed it to test.py and all test.py , it had nothing to do with the location of the package, but only with the file name.

0
source

All Articles