简体   繁体   中英

Having Issue Importing scapy.all in my file

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". 在此处输入图片说明

  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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM