Problem with nmap in python
Solvedshodannn Posted messages 10 Status Member -
Hello,
I have a problem with the nmap module in Python. After installing the required modules for nmap, my import nmap does not work. It says there is no module named nmap.
Here is my code:
import nmap sc = nmap.PortScanner() def main(): n = input("1 - Network scanner \n2 - Vulnerabilities Detection \n3 - Exploit\n Enter a number :") if n == '1': nmap() def nmap(): ip = input("\n enter ip adress:") sc.scan(ip,'1-8080') print(sc.scaninfo()) print(sc[ip]['tcp'].keys()) if __name__ == "__main__": main() And here is the error in the console:
line 1, in <module> import nmap ^^^^^^^^^^^ ModuleNotFoundError: No module named 'nmap' Thank you for your help
2 answers
Good evening,
Which library did you install and how?
On pypi https://pypi.org/search/?q=nmap there are 2.
What system are you on? Are you sure everything went well during the installation of this library?
You can already check if this module appears in the list of installed modules.
python -m pip list
Hello,
It seems that your pip installed nmap for python312. Are you sure that this is the Python interpreter you are using to run your program?
You need to clarify how you are launching your program. If it's through the Windows explorer, there is no guarantee that this interpreter is being called. If your project is created in an IDE like PyCharm and the project is stored in a virtual environment, that could also explain why the import is not working.
At the launch of your program, you can check the value of the sys.path variable. This will allow you to see in which directories are being examined by the Python interpreter used to run the program.
Example (on Linux, but the principle remains the same on Windows):
import sys print(sys.path)
['/home/mando/.local/bin', '/usr/lib/python312.zip', '/usr/lib/python3.12', '/usr/lib/python3.12/lib-dynload', '', '/home/mando/.local/lib/python3.12/site-packages', '/usr/local/lib/python3.12/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.12/dist-packages']
Here we can see the different folders being examined by the Python interpreter. We also note that in this example it is a python3.12 interpreter. If I execute the instruction :
import nmap
... then the Python interpreter will iterate over each of these folders, in order, until it finds a folder nmap (which is supposed to contain a file __init__.py). The interpreter stops as soon as it finds one (there is thus a notion of priority if the nmap package is installed in multiple places). If none of these folders work, then the import fails (which is your case). If your nmap module is not present in any of these folders, it is either not installed (for the Python interpreter currently running), or its installation is incorrect.
If the problem persists, please report the value of your sys.path and check in which folder (absolute path) nmap is installed.
Good luck
It's good, I finally found it. In fact, there was a new version of pip, and when I wanted to update it, it kept saying access denied. So I just realized that if I launched VS Code as admin, I could update it, and suddenly, no more issues with import nmap.
Thank you very much for taking the time to help me
Have a good evening
I installed the python-nmap library 0.7.1, I'm on Windows, and yes, when I try to reinstall it, it says in the console "Requirement already satisfied: python-nmap in c:\python312\lib\site-packages (0.7.1)"
If it helps, here is the list of modules I have:
Package Version
------------------ --------
beautifulsoup4 4.12.3
certifi 2024.6.2
charset-normalizer 3.3.2
idna 3.7
pip 24.1.2
python-nmap 0.7.1
requests 2.32.3
soupsieve 2.5
urllib3 2.2.1