Python raises the ModuleNotFoundError: No module named ‘SimpleHTTPServer’ error when you are trying to use the SimpleHTTPServer
module, which is available in Python 2 but not in Python 3.
To fix the ModuleNotFoundError: No module named ‘SimpleHTTPServer’ error, you should use the http.server module instead of the SimpleHTTPServer module.
import http.server
import socketserver
PORT = 8000
handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), handler) as httpd:
print("Serving on port", PORT)
httpd.serve_forever()
Output
Serving on port 8000
127.0.0.1 - - [20/Mar/2023 03:22:50] "GET / HTTP/1.1" 200 -
You can see that it started a simple HTTP server on port 8000, serving the files from the current directory.
To access the server, open a web browser and navigate to http://localhost:8000. The server will display a list of files in the current directory.
Remember that the SimpleHTTPServer module is for Python 2, while the http.server module is for Python 3.
If you’re using Python 3, you should use the http.server module, as shown in the example above.
That’s it.