简体   繁体   English

在我的文件中导入 scapy.all 时出现问题

[英]Having Issue Importing scapy.all in my file

I am having issues when importing scapy in my Ubuntu virtual machine.在我的 Ubuntu 虚拟机中导入 scapy 时遇到问题。
I can easily use from scapy.all import * in the terminal.我可以在终端中轻松使用from scapy.all import *
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.我正在使用 Visual Studio Code,当我将鼠标悬停在 scapy 上时,右键单击并转到定义,它会将我带到 scapy 文件。
Here's what solutions I've seen and tried:以下是我见过并尝试过的解决方案:

  1. My filename(or any filename in the folder) is not "scapy.py".我的文件名(或文件夹中的任何文件名)不是“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".我直接从网站上安装了 scapy,使用了“sudo apt-get install python3-scapy”,还尝试了“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.如果我运行另一个 python 文件(没有“sudo”和“from scapy.all import *”),它运行良好。 But there is an import issue when I use "sudo".但是当我使用“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 \\ 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.所以我尝试在一个新的 VirtualBox 镜像上做所有的事情。 But that also failed.但这也失败了。

  6. I can type "scapy" in the terminal and it opens up fine.我可以在终端中输入“scapy”,它打开得很好。

my python version is 3.8.10我的 python 版本是 3.8.10
Scapy version: 2.4.5 Scapy 版本:2.4.5

I've been stuck on this for 4 days.我已经被困在这个问题上 4 天了。 Please help.请帮忙。

it's recommended to use virtualenv , it will resolve the conflict if you have multiple python versions.建议使用virtualenv ,如果您有多个 python 版本,它将解决冲突。

  • 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安装scapy:

    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安装在非 root 用户上的软件包未在系统范围内安装,因此当您以 root 用户身份运行脚本时会出现导入问题
Some solutions that can be used:一些可以使用的解决方案:

  1. Install scapy to root user too也将scapy安装到root用户
  • Switch to root切换到root
sudo su
  • Install scapy安装 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或者,创建一个虚拟环境推荐)并在那里安装 scapy
  • 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通过pip将scapy安装到新的虚拟环境中
(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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