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:
I saw this when I had a scapy.py
in the current directory. scapy.all import *
seems to look in the current directory first.
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.
Quite old post. For the ones still looking, the correct import with current versions would be:
from scapy.all import *
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.
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 scappy. 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.