user17825029
user17825029

Reputation:

Getting error: AttributeError: module 'nmap' has no attribute 'PortScanner'

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:

  1. Uninstalled and reinstalled python-nmap as well as just nmap (since they are interconnected).
  2. I've tried renaming the module itself.
  3. I've launched my code on different IDEs
  4. 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

Upvotes: 1

Views: 4655

Answers (2)

Brandon Aruffo
Brandon Aruffo

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

gremur
gremur

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

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'}},
...

Upvotes: 4

Related Questions