{"id":7630,"library":"python-nmap","title":"python-nmap","description":"python-nmap is a Python library that provides a convenient interface for interacting with the Nmap security scanner. It allows Python scripts to build and execute Nmap scans, parse scan results, and automate network discovery and vulnerability assessment tasks. The library is currently at version 0.7.1 and has an irregular release cadence, with updates addressing bugs and improving compatibility.","status":"active","version":"0.7.1","language":"python","source_language":"en","source_url":"https://github.com/nmmapper/python3-nmap","tags":["nmap","network-scanning","security","pentesting","network","automation"],"install":[{"cmd":"pip install python-nmap","lang":"bash","label":"Install via pip"}],"dependencies":[{"reason":"This Python library is a wrapper around the Nmap executable. Nmap must be installed on your system and accessible in the system's PATH for python-nmap to function.","package":"Nmap (system tool)","optional":false}],"imports":[{"wrong":"import nmap","symbol":"PortScanner","correct":"from nmap import PortScanner"}],"quickstart":{"code":"import nmap\nimport os\n\n# Instantiate the PortScanner\nnm = nmap.PortScanner()\n\n# Define target and ports\ntarget_host = os.environ.get('SCAN_TARGET_IP', '127.0.0.1')\nscan_ports = os.environ.get('SCAN_PORTS', '22-443')\n\ntry:\n    # Perform a scan\n    nm.scan(target_host, scan_ports)\n\n    # Iterate through scanned hosts\n    for host in nm.all_hosts():\n        print(f'Host : {host} ({nm[host].hostname()})')\n        print(f'State : {nm[host].state()}')\n        for proto in nm[host].all_protocols():\n            print('----------')\n            print(f'Protocol : {proto}')\n\n            lport = nm[host][proto].keys()\n            for port in lport:\n                print(f'port : {port}\\tstate : {nm[host][proto][port]['state']}')\nexcept nmap.nmap.PortScannerError as e:\n    print(f\"Error during scan: {e}\")\n    print(\"Please ensure Nmap is installed and in your system's PATH.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to perform a basic port scan on a target IP address and then print the host information, state, and details for all discovered open ports and their services. It includes basic error handling for common Nmap-related issues."},"warnings":[{"fix":"Install Nmap on your operating system (e.g., `sudo apt install nmap` on Debian/Ubuntu, or download from nmap.org for Windows/macOS) and ensure its executable path is configured correctly in your system's PATH environment variable.","message":"Nmap executable (the system tool) must be installed and in your system's PATH. This Python library is a wrapper and will not work if Nmap is not found. This is the most common reason for errors.","severity":"breaking","affected_versions":"All versions"},{"fix":"Rename your Python script to something other than `nmap.py` (e.g., `my_scanner.py`). If the problem persists, delete any `.pyc` files or `__pycache__` directories related to the misnamed script.","message":"Naming your Python script `nmap.py` will cause an `AttributeError: module 'nmap' has no attribute 'PortScanner'`. This is because Python will try to import your own script instead of the installed `python-nmap` library.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Refactor asynchronous scan logic to use `PortScanner().iterscan()` or implement custom `asyncio` with `subprocess` calls. Review the latest GitHub README for current async patterns.","message":"Older versions of `python-nmap` included `PortScannerAsync` for asynchronous scanning. This class was removed, and asynchronous operations are now typically handled via the `iterscan` method within the `PortScanner` class, or by using Python's `asyncio` with `subprocess` for more fine-grained control.","severity":"deprecated","affected_versions":"< 0.6.3 (roughly 2018-2019)"}],"env_vars":null,"search_vec":"'0.7.1':50 'address':59 'allow':24 'assess':42 'autom':37,71 'bug':60 'build':28 'cadenc':56 'compat':63 'conveni':14 'current':47 'discoveri':39 'execut':30 'improv':62 'interact':17 'interfac':15 'irregular':54 'librari':10,45 'network':38,66,70 'network-scan':65 'nmap':3,6,20,31,64 'pars':33 'pentest':69 'provid':12 'python':2,5,9,25 'python-nmap':1,4 'releas':55 'result':35 'scan':32,34,67 'scanner':22 'script':26 'secur':21,68 'task':43 'updat':58 'version':49 'vulner':41","created_at":"2026-04-16T14:06:37.271566+00:00","updated_at":"2026-04-16T14:06:37.271566+00:00","problems":null,"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.7.1","cli_name":"","cli_version":null,"type":"library","homepage":"http://xael.org/pages/python-nmap-en.html","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/python-nmap/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["http-networking","devops"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-30","last_verified":"2026-06-30","next_check":"2026-07-30","install_tag":null}}