14

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.

Michael Nielsen
  • 1,194
  • 3
  • 22
  • 37

6 Answers6

20

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

nvsk. avinash
  • 581
  • 1
  • 5
  • 10
10

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.

Chen A.
  • 10,140
  • 3
  • 42
  • 61
  • This doesn't work on Mojave. There is no scapy in /usr/local/lib/python2.7/site-packages – JD Allen Jan 04 '21 at 18:45
  • You need to figure out where pip installed scapy, and add this directory. – Chen A. Jan 05 '21 at 07:37
  • Thanks. This is a valid solution, at least for me. I am using CentOS7.6 and python3.6. After using "yum install scapy". The location is "/usr/lib/python3.6/site-packages/". If using pip3 or building python from source code, it may have a different location. – Polymersudo Jun 02 '22 at 07:27
4
  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 *
    
3

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!

aakriti
  • 98
  • 1
  • 7
2

If you are using termux maybe you shoud try this:

pip2 install scapy.
Dharman
  • 30,962
  • 25
  • 85
  • 135
theautistic9
  • 69
  • 1
  • 1
  • 5
0

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")
Andrew
  • 816
  • 7
  • 15