Python nmap program was not found in path error even nmap installed and nmap.exe is in the path

Question:

For some reason I can’t use nmap on python. I have python-nmap installed as a library. Whenever I try to use PortScanner(), I get path error but I have nmap installed on my computer and it is even in path.

import nmap

nm = nmap.PortScanner()

print(nm)

The error is

raise PortScannerError(
nmap.nmap.PortScannerError: 'nmap program was not found in path. PATH is : C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\nodejs\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files (x86)\dotnet\;C:\Users\Anti\AppData\Local\Programs\Python\Python38\Scripts\;C:\Users\Anti\AppData\Local\Programs\Python\Python38\;C:\Program Files\Java\jdk-13.0.2\bin;C:\Users\Anti\Desktop\Selenium;C:\Users\Anti\Desktop\C++;C:\ffmpeg\bin;;C:\Users\Anti\AppData\Local\Programs\Microsoft VS Code\bin;C:\MinGW\bin;C:\Users\Anti\AppData\Roaming\npm;C:\Users\Anti\.dotnet\tools;C:\Program Files (x86)\Nmap'

My nmap files are in the path as you can see.

C:\Program Files (x86)\Nmap

I tried to reinstall nmap and add in path again, nothing changed.
I can use the nmap command directly on cmd without any problems. By the way, I don’t use virtualenv. How can I solve this?

Asked By: SyntaxError

||

Answers:

You need to add path for nmap.exe while initializing PortScanner

import nmap

nmap_path = r"C:Program Files (x86)Nmapnmap.exe"
nmap.PortScanner(nmap_search_path = nmap_path)
Answered By: sushanth

I know this is old thread. But if someone is searching for similar answer.
What @sushanth answered is correct.
It just has to be list:

nmap_path = [r"C:Program Files (x86)Nmapnmap.exe",]
scanner = nmap.PortScanner(nmap_search_path=nmap_path)
Answered By: Jasec

this article helped a lot, because I had the same issue.
But there are still some things to be respected:

  • Ensure that nmap_path is a list or a tuple, not a single str.
  • when defining the path, it needs to be noted with a leading dot "."
import nmap # networ scanner "nmap" needs also the nmap tool from https://nmap.org/download

# nmap ping scan
#s_path=(r'C:Program Files (x86)Nmapnmap.exe',r'Nmapnmap.exe',r'nmap-7.92nmap.exe')
s_path=[r'Nmapnmap.exe',r'.nmap-7.92nmap.exe']
print(type(s_path))
nm = nmap.PortScanner(nmap_search_path = s_path) # alternative way with seld defined path
#nm = nmap.PortScanner() #standard way, if Nmap tool is installed correct from https://nmap.org/download
nm.scan(hosts="192.168.178.0/24",arguments="-sn")
ip_list=nm.all_hosts() #returns a list with all available IPs
print(ip_list)
Answered By: Mario DeMarco
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.