Solved [Python3.7 + Scapy] Class from Scapy is not found

hi,
I'm a lecturer from Indonesia and I'm teaching cybersecurity course.
I need to use Python and Scapy to do sniffing and snooping, so I installed:
Code:
# /usr/ports/lang/python37
# /usr/ports/net/scapy
Python3.7 works fine, however when I try to use any class from Scapy, it said "NameError: name <class< is not defined"
Code:
$ python3.7
Python 3.7.9 (default, Apr 26 2021, 05:45:03)
[Clang 10.0.1 ([email]git@github.com[/email]:llvm/llvm-project.git llvmorg-10.0.1-0-gef32c611a on freebsd12
Type "help", "copyright", "credits" or "license" for more information.
>>> from scapy.all import *
>>> a=IP()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'IP' is not defined
>>> dir(scapy)
['AnyStr', 'VERSION', 'VERSION_MAIN', '_SCAPY_PKG_DIR', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_tmp', '_version', '_version_from_git_describe', 'all', 'ansmachine', 'arch', 'as_resolvers', 'asn1', 'asn1fields', 'asn1packet', 'automaton', 'autorun', 'base_classes', 'compat', 'config', 'consts', 'dadict', 'data', 'error', 'extlib', 'fields', 'layers', 'libs', 'main', 'modules', 'os', 'packet', 'pipetool', 'plist', 'pton_ntop', 're', 'route', 'scapypipes', 'sendrecv', 'sessions', 'subprocess', 'supersocket', 'themes', 'utils', 'utils6', 'volatile']
 
Note that the default python version changed from 3.7 to 3.8 recently.

See /usr/ports/UPDATING:
Code:
20210425:
  AFFECTS: users of python
  AUTHOR: kai@FreeBSD.org

  The default version of python3 and python was switched to 3.8.

  For ports users wanting to keep version 3.7 as default,
  add DEFAULT_VERSIONS+= python=3.7 python3=3.7 to make.conf

  Following procedures may ease the upgrade:

  For users of pre-build packages:
  # sh
  # for i in $(pkg query -g %n 'py37-*'); do pkg set -yn ${i}:py38-${i#py37-}; done
  # pkg upgrade

  For portmaster users:
  # sh
  # portmaster -o lang/python38 python37
  # REINSTALL="$(pkg info -o py37-\* | awk '{printf "%s ", $2}')"
  # pkg delete -f py37-\*
  # portmaster $REINSTALL
  # REBUILD=$(pkg query -g "%n:%dn" '*' | grep py3 | grep -v py38 | cut -d : -f 1 | sort -u)
  # portmaster $REBUILD
  # REBUILD2=$(pkg list | grep python-37 | xargs pkg which | awk '{print $6}' | sort -u)
  # portmaster $REBUILD2
 
Works fine though:
Code:
dice@molly:~ % python3.8
Python 3.8.9 (default, Apr 26 2021, 10:47:41)
[Clang 11.0.1 (git@github.com:llvm/llvm-project.git llvmorg-11.0.1-0-g43ff75f2c on freebsd13
Type "help", "copyright", "credits" or "license" for more information.
>>> from scapy.all import *
>>> a=IP()
>>>
 
thank you all for the response,
looks like in my case another port is using Python3.7 as dependency
so in my machine I have Python3.7 and also Python3.8
and when I try to install Scapy using:

Code:
# cd /usr/ports/net/scapy
# make install clean

it looks like Scapy choose Python3.7 as its default

So, what I did is:
  1. create a fresh installed FreeBSD (I'm using VM)
  2. install Python3.7 from /usr/ports/lang/python37
  3. install "pip" and then use "pip" to install to Scapy
Code:
# cd /usr/ports/devel/py-pip
# make install clean
# pip install --pre scapy

and now I can use Scapy on my Python3.7

Code:
$ python3.7
Python 3.7.9 (default, May  6 2021, 04:34:40)
[Clang 10.0.1 (git@github.com:llvm/llvm-project.git llvmorg-10.0.1-0-gef32c611a on freebsd12
Type "help", "copyright", "credits" or "license" for more information.
>>> from scapy.all import *
>>> a=IP()
>>> a.show()
###[ IP ]###
  version   = 4
  ihl       = None
  tos       = 0x0
  len       = None
  id        = 1
  flags     =
  frag      = 0
  ttl       = 64
  proto     = ip
  chksum    = None
  src       = 127.0.0.1
  dst       = 127.0.0.1
  \options   \

If you don't mind, I'm going to put the full solution with both Python3.7 and Python3.8 to my blog
 
Last edited:
Back
Top