问题
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.
回答1:
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.
回答2:
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'] >>> $
- Identify the location of scapy on you box
$ which scapy /usr/bin/scapy $
- 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:
If you are using termux maybe you shoud try this:
pip2 install scapy.
来源:https://stackoverflow.com/questions/46602880/importerror-no-module-named-scapy-all