0

I am having issues when importing scapy in my Ubuntu virtual machine.
I can easily use from scapy.all import * in the terminal.
I am using Visual Studio Code and when I hover over scapy, right click and go to defintion, it takes me to the scapy file.
Here's what solutions I've seen and tried:

  1. My filename(or any filename in the folder) is not "scapy.py".

  2. I've installed scapy directly from the website, used "sudo apt-get install python3-scapy" and also tried with "pip install scapy". None of which changed anything.

  3. If I run another python file (without "sudo" and with "from scapy.all import *") it runs fine. But there is an import issue when I use "sudo". enter image description here

  4. I tried to do the following to solve my issue:

    • Type in the terminal:
      sudo mkdir /usr/lib/python2.7/dist-packages/scapy
      cd /usr/lib/python3/dist-packages/
      cp -avr scapy/* /usr/lib/python2.7/dist-packages/scapy\

    This also didnt work out.

  5. I also thought maybe I messed up some of the module files. So I tried doing everything on a new VirtualBox image. But that also failed.

  6. I can type "scapy" in the terminal and it opens up fine.

my python version is 3.8.10
Scapy version: 2.4.5

I've been stuck on this for 4 days. Please help.

Omar Al-Howeiti
  • 1,227
  • 7
  • 19
StwayneXG
  • 1
  • 2
  • what is your default python version? can you run ```python --version``` and check what is the version? – Omar Al-Howeiti Jul 19 '21 at 22:48
  • it's not recommended to use sudo while working with python, you should use virtualenv instead. – Omar Al-Howeiti Jul 19 '21 at 23:11
  • I am working on an assignment and had to run everything through a makefile. So, I don't have much control over running the files. Anyways, Installing scapy to root user solved the problem. Thank you. – StwayneXG Jul 20 '21 at 09:30

2 Answers2

0

it's recommended to use virtualenv, it will resolve the conflict if you have multiple python versions.

  • create a virtual environment, by the following commands

    virtualenv env -p python3

  • then activate it

    source env/bin/activate

  • then install scapy using pip:

    pip install scapy

and try to import the library from there.

Omar Al-Howeiti
  • 1,227
  • 7
  • 19
0

Packages installed on a non-root user are not installed system wide, thus there is an import issue when you run the script as a root user
Some solutions that can be used:

  1. Install scapy to root user too
  • Switch to root
sudo su
  • Install scapy
pip install scapy
  • You can then run your script locally and no issues brought up
sudo python3 script.py
  1. Alternatively, create a virtual environment(recommended) and install scapy there
  • Create a virtual environment
demo@stack:~/demo$ virtualenv venv
  • Activate the virtual environment
demo@stack:~/demo$ source venv/bin/activate
  • Install scapy via pip into the new virtual environment
(venv) demo@stack:~/demo$ pip install scapy
  • You can now run your script with superuser permissions without issue
(venv) demo@stack:~/demo$ sudo python3 script.py
boynamedboy
  • 1
  • 1
  • 1
  • you are right expect the last line, you shouldn't use ```sudo``` at all, so if you are using virtualenv, use ```python script.py ``` , using sudo will use the system root python not the one in the virtualenv. – Omar Al-Howeiti Jul 20 '21 at 20:59