Reputation:
I have been seeking for answers of how to fix this Python problem:
AttributeError: module 'nmap' has no attribute 'PortScanner'
I wanted to learn more about port-scanning but I couldn't even install the module on Visual Studio Code, which I am using. I've tried everything that I and many people can think of:
No success so far..
This is my code:
import nmap
nm = nmap.PortScanner()
nm.scan('127.0.0.1', '22-443')
and the output:
/usr/local/bin/python3 /Users
/user2132/Desktop/PYTHONProjects/portscannning.py
Traceback (most recent call last):
File "/Users/user2132/Desktop/PYTHONProjects/portscannning.py", line 3, in <module>
nm = nmap.PortScanner()
AttributeError: module 'nmap' has no attribute 'PortScanner'
What can I try next?
P.S. I am using MacOS
Upvotes: 1
Views: 4655
Reputation: 1
Try Ctrl+Shift+P, type env, then create a new environment and add py -3 -m venv .venv
and press Enter.
Upvotes: -1
Reputation: 1690
I was able to reproduce the error. The problem was with the nmap
library. pip install nmap
installs nmap python library
but python-nmap
requires nmap binary
, moreover nmap
python library conflicts with python-nmap
because they share same module name. The correct nmap
could be installed from Nmap's official download page
pip uninstall nmap
pip uninstall python-nmap
python-nmap
pip install python-nmap
nmap
installed into your systemwhich nmap
Go to Nmap's official download page, download and install nmap for your OS.
Please make sure that add to PATH
option is selected during installation.
Check nmap
installation with which nmap
command in terminal.
After that you can check if PortScanner
is in nmap
.
import nmap
dir(nmap)
Returns
['ET',
'PortScanner', <=== IS HERE!
'PortScannerAsync',
'PortScannerError',
'PortScannerHostDict',
'PortScannerTimeout',
'PortScannerYield',
'Process',
'__author__',
'__builtins__',
'__cached__',
'__doc__',
'__file__',
'__last_modification__',
'__loader__',
'__name__',
'__package__',
'__path__',
'__spec__',
'__version__',
'convert_nmap_output_to_encoding',
'csv',
'io',
'nmap',
'os',
're',
'shlex',
'subprocess',
'sys']
Final test
import nmap
nm = nmap.PortScanner()
nm.scan('127.0.0.1', '22-443')
Returns
{'nmap': {'command_line': 'nmap -oX - -p 22-443 -sV 127.0.0.1',
'scaninfo': {'tcp': {'method': 'syn', 'services': '22-443'}},
'scanstats': {'timestr': 'Tue Mar 29 15:07:02 2022',
'elapsed': '7.82',
'uphosts': '1',
'downhosts': '0',
'totalhosts': '1'}},
...
Upvotes: 4