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

2
  • Possible post duplicate (exact title duplicate): stackoverflow.com/questions/50992325/… Commented Mar 28, 2022 at 19:17
  • i've tried, it doesn't work :( Commented Mar 28, 2022 at 19:18

2 Answers 2

4

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'}},
...
Sign up to request clarification or add additional context in comments.

11 Comments

File "/Users/user2132/Desktop/PYTHONProjects/test.py", line 4, in <module> 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'
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 :(
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
it seems like nmap conflicts with python-nmap because they share the same module name. let me see how it works in my environment
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
|
-1

Try Ctrl+Shift+P, type env, then create a new environment and add py -3 -m venv .venv and press Enter.

1 Comment

I have amended this to include the actual solution, which was given in a comment (answers are permitted to indicate what was wrong, but they must offer a solution to the problem).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.