14

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 the 'try' because of another topic here (tried it as a solution). My current output with this code is the following

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 trying it just with from scapy.all import * withouth the '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 different ways of importation found on Google but it still doesn't work. Could anyone please tell me what I'm doing wrong? (don't mind the indentation of this post)

Nicholas
  • 1,189
  • 4
  • 20
  • 40

5 Answers5

8

From looking at scapy source, the scapy package doesn't appear to import anything or define an __all__ in __init__. As a result, you need to explicitly import scapy.all (or from scapy import all) before you can from scapy.all import anything else from it, as it won't be in sys.modules yet. Note that this only has to happen once in your program flow though, as after the interpreter imports the module, it will be available to all code that executes from then on, regardless of where it is. Take a look at the Python docs on modules and how import, and specifically importing a package, works for more details.

Edit: I think I see the problem now, I just was paying attention to the wrong part of your stack trace. Pretty sure what you are dealing with here is a name collision. Your file is named scapy.py, so when you import scapy from the context of that file, you are actually importing the file itself as a module. Since your file does not have a submodule named all (it can't, since it's not a package), you get the import error you are seeing. Try switching the name of your file to something that does not conflict with any packages or modules you wish to import inside it, and see if that works out better.

By the way, note in your stack traces that your import is actually essentially recursively calling your one file. That should be a clue that something has gone haywire in the import process.

Silas Ray
  • 25,682
  • 5
  • 48
  • 63
  • 3
    This does not really help me out, still getting something like 'No module named all' – Nicholas Nov 28 '12 at 17:47
  • Edited my answer, I think that should fix your problem. – Silas Ray Nov 28 '12 at 20:17
  • Well we're getting somewhere. But when I changed the name it was complaining about a 'bad magic number'. I deleted scapy.pyc as in http://stackoverflow.com/questions/514371/whats-the-bad-magic-number-error . But how do you get it back with right magic number? *EDIT, seems to work now! I put python3 in front of the script execution but that didn't seem to be necessary, thanks! – Nicholas Nov 29 '12 at 08:23
  • Seriously, THANKS. Would have never figured out the problem was to name my test file scapy – martinvigo Jan 10 '16 at 22:49
3

I like to add something to @Daniel answer. Your real problem is not scapy package. Your real problem is in your python file name. Don't ever use library name or its contents as your file name.

In your case, your file name is scapy.py. After that you import scapy. In here you accidentally call your python file as object in your code there for your compiler can't understand which type(file or library) to call. There for that error was appeared.

Kalana
  • 5,631
  • 7
  • 30
  • 51
2

I had a similar problem on OSX, I installed the scapy package pip install scapy and then I was trying to execute my test file scapy.py The error I got 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 itself that caused the problem it can't be called scapy.py. I change it to test.py and all worked, it had nothing to do with the package location just the file name.

dev_dan
  • 89
  • 6
1

I saw this when I had a scapy.py in the current directory. scapy.all import * seems to look in the current directory first.

Mp0int
  • 18,172
  • 15
  • 83
  • 114
chris77
  • 11
  • 1
1

The correct import with current versions would be:

from scapy.all import *
galoget
  • 722
  • 9
  • 15
Cukic0d
  • 5,111
  • 2
  • 19
  • 48
  • With scapy 2.4.3 and Python 3.7 still beside import * from all I need import e.g. from scapy.layers.inet import IP, UDP, in4_chksum – Adam Mierzwiak Aug 19 '20 at 08:31