1

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

1 Answers1

3

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'}},
...
gremur
  • 1,645
  • 2
  • 7
  • 20
  • File "/Users/user2132/Desktop/PYTHONProjects/test.py", line 4, in nm = nmap.PortScanner() File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/nmap/nmap.py", line 136, in __init__ raise PortScannerError( nmap.nmap.PortScannerError: 'nmap program was not found in path. PATH is : /Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.10/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.10/bin' – Stanford Martinez Mar 29 '22 at 11:28
  • After following your steps, I indeed have the same output with dir(nmap) as you wrote, but the second I run my code, I get this error :( – Stanford Martinez Mar 29 '22 at 11:30
  • After your last edit, something indeed became different, but I was just wandering, is package "wheel" a necessary thing to have installed? I get this: Collecting python-nmap==0.6.4 Using cached python-nmap-0.6.4.tar.gz (43 kB) Preparing metadata (setup.py) ... done Using legacy 'setup.py install' for python-nmap, since package 'wheel' is not installed. Installing collected packages: python-nmap Running setup.py install for python-nmap ... done Successfully installed python-nmap-0.6.4 – Stanford Martinez Mar 29 '22 at 11:41
  • it seems like `nmap` conflicts with `python-nmap` because they share the same module name. let me see how it works in my environment – gremur Mar 29 '22 at 11:44
  • I was able to reproduce the error. The problem was with the `nmap` 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](https://nmap.org/download.html) – gremur Mar 29 '22 at 12:37
  • Downgrade is not required in this case i believe – gremur Mar 29 '22 at 12:43
  • man, thank you so much for all your help and your time, but now, after all your four steps, it simply does not launch the code. It's simply blank. I've checked my 'which nmap', which returns /usr/local/bin/nmap – Stanford Martinez Mar 29 '22 at 13:33
  • hmm, it seems like `nmap binary` is in place and `python-nmap` has been properly installed. Are you running your code in jupyter notebook or in IDE? I think the issue with blank output is related with environment but not with nmap. – gremur Mar 29 '22 at 14:39
  • I’m running my code in Visual Studio Code IDE. Never tried Jupiter notebook – Stanford Martinez Mar 29 '22 at 17:04
  • I hope it helps you [Visual Studio Code is not showing the output of Python](https://stackoverflow.com/questions/49529153/visual-studio-code-is-not-showing-the-ouput-of-python) – gremur Mar 30 '22 at 05:10
  • Yeah, it finally worked!! I am so thankful for all your help, brother! – Stanford Martinez Mar 30 '22 at 11:03