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:
- Uninstalled and reinstalled python-nmap as well as just nmap (since they are interconnected).
- I've tried renaming the module itself.
- I've launched my code on different IDEs
- I've created a separate folder and put modules and my project there.
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
-
Possible post duplicate (exact title duplicate): stackoverflow.com/questions/50992325/…halfer– halfer2022年03月28日 19:17:00 +00:00Commented Mar 28, 2022 at 19:17
-
i've tried, it doesn't work :(user17825029– user178250292022年03月28日 19:18:11 +00:00Commented Mar 28, 2022 at 19:18
2 Answers 2
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
Please follow the steps below:
Step 1. uninstall libraries
pip uninstall nmap
pip uninstall python-nmap
Step 2. install python-nmap
pip install python-nmap
Step 3. Check if nmap installed into your system
which nmap
Step 3. If it is installed, continue to the next step, if not:
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.
Step 4. Reboot your system (restart computer)
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'}},
...
11 Comments
nmap conflicts with python-nmap because they share the same module name. let me see how it works in my environmentnmap library. pip install nmap installs the wrong library for python-nmap, moreover this nmap conflicts with python-nmap. The correct nmap library must be installed from Nmap's official download page Try Ctrl+Shift+P, type env, then create a new environment and add py -3 -m venv .venv and press Enter.