Michael Nielsen
Michael Nielsen

Reputation: 1242

ImportError: No module named scapy.all

I'm running macOS Sierra and Python 2.7.

In my terminal I've installed scapy with:

pip install scapy
Requirement already satisfied: scapy in /usr/local/lib/python2.7/site-packages

But running this:

from scapy.all import *

for pkt in sniff(iface='en0'):
    print pkt

Gives me this:

python test.py 
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from scapy.all import *
ImportError: No module named scapy.all

I've tried and Google around, and installed pcapy and other packages - but no luck.

Upvotes: 15

Views: 122623

Answers (7)

okaeiz
okaeiz

Reputation: 390

Simply install the module using apt:

sudo apt update
sudo apt upgrade
sudo apt install python3-scapy

Upvotes: 0

Andrew
Andrew

Reputation: 836

If you have scapy installed, you can use code such as the code below. You do not need to wrap this in a try block (obviously), but that works for me today.

try:
   from scapy.all import rdpcap, IP, Ether, PacketList
except ImportError:
   raise ImportError('scapy is not installed, please install it by running: '
          'pip install scapy') from None

Now you want to do something?

plist = rdc.ap(sys.argv[1])
for data in plist:
    if isinstance(data, Ether):
        print("Found an ether object, might be what I want")

Upvotes: 0

aakriti
aakriti

Reputation: 158

Most of the answers here are helping you to install scapy on system-level and run with sudo access. Using sys.path they're trying to add the path to the installed scapy location, basically, pip install packages in lib/python<any-version>/site-packages/ since python interpreter you're using to run the program containing the use of scapy python package don't have scapy in site-packages that's why interpreter is raising ImportError: No module named scapy.all!

I would recommend using a virtual environment manager to create a separate virtual environment of your choice of python version which could be 2.7 or 3.8 or whatever and then install and use scapy python package for your project inside your virtual environment. This will keep you system clean and the problem would be easily debuggable, you can delete this virtual environment later, and there will be no trace left of the project dependencies.

The use is very straight-forward:

assuming you've virtualenv(external package like conda) installed, if not you can install

  • if on python(version 2.x) : pip install virtualenv
  • if on python(version 3.x) : python3 -m pip install virtualenv

Then,

create a virtual environment, you can name anything!

virtualenv --python=3.8 packet_sniffer_env38

activate virtual environment

source packet_sniffer_env38/bin/activate

simply install your dependencies, on your case scapy

pip install scapy

Now you can run a program using scapy, note you still need to use sudo access but this time you'll be using the python interpreter of the virtual environment and not of your system.

Also, you can check the libraries of this virtual environment, ./packet_sniffer_env38/lib/python3.8/site-packages and you will find scapy here!

sudo ./packet_sniffer_env38/bin/python <your_program_using_scapy>.py

you can have a look at the working use of scapy python package with virtualenv!

Upvotes: 3

nvsk. avinash
nvsk. avinash

Reputation: 611

If you are using ubuntu type: 'sudo apt-get install python3-scapy' for python version >= 3.

Upvotes: 21

theautistic9
theautistic9

Reputation: 69

If you are using termux maybe you shoud try this:

pip2 install scapy.

Upvotes: 2

user6815864
user6815864

Reputation:

  1. Identify the location where Python is importing its libraries from

    $ python
    Python 2.7.15+ (default, Aug 31 2018, 11:56:52)
    [GCC 8.2.0] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> print os.sys.path
    ['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/wx-3.0-gtk3']
    >>>
    $
    
    1. Identify the location of scapy on you box
    $ which scapy
    /usr/bin/scapy
    $
    
    1. You will no longer get the import error
    $ python
    Python 2.7.15+ (default, Aug 31 2018, 11:56:52)
    [GCC 8.2.0] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.sys.path.append('/usr/bin/')
    >>> from scapy.all import *
    

Upvotes: 4

Chen A.
Chen A.

Reputation: 11280

ImportError: No module.. found error happens when Python doesn't find your module. So, where does it look for modules?

import os
print os.sys.path

Verify /usr/local/lib/python2.7/site-packages is in that list. If not, append it

os.sys.path.append('/usr/local/lib/python2.7/site-packages') and try to load it. If that still doesn't work, try re-installing the module, because it seems there is an issue there.

Upvotes: 10

Related Questions