pythonhttpserversimplehttprequesthandler

ModuleNotFoundError: No module named 'http.server'; 'http' is not a package


I am trying to setup http server on my machine but I get the error:

ModuleNotFoundError: No module named 'http.server'; 'http' is not a package

I have 2 files in my project directory: http.py and index.html.

Here is the http.py:

import http.server
import socketserver

PORT = 8080
Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

I have already tried changing module to BaseHTTPServer and I get this error:

ModuleNotFoundError: No module named 'BaseHTTPServer'

I also notice a weird thing happen on my terminal. If I try do

python3 -m pip uninstall <module>

I get an error such as

ModuleNotFoundError: No module named 'http.server'; 'http' is not a package

which is throwing me off because I am not even running the file. I mention this in case it is any indication that some local configuration might be the problem of all.


Solution

  • You have named your file as http.py. This is overriding the standard library http module. To solve:

    It may interest you to read up on how modules and packages work in Python.