Issue with importError: no module named BaseHTTPServer
Solution 1
Are you sure that the python you are running/testing from the command line is the same python that your script is running?
i.e. BaseHTTPServer might be present in one install, but not the other.
For example, on my machine:
$ which python2.7
/usr/bin/python2.7
Is your "python" (in the command line you specified) the same as "python2.7"?
$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import BaseHTTPServer
>>> BaseHTTPServer
<module 'BaseHTTPServer' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.pyc'>
Try a module that does exist to ensure the path is what you are expecting.
Solution 2
You may consider switching to python 3.x. This is the updated version of your code in Python 3.7.2. Notice in the code below that the BaseHTTPServer
module is changed to http.server
in python3 link
from http.server import BaseHTTPRequestHandler, HTTPServer
class WebServerHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path.endswith("/hello"):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
message = ""
message += "<html><body>Hello!</body></html>"
self.wfile.write(message)
print (message)
return
else:
self.send_error(404, 'File Not Found: %s' % self.path)
def main():
try:
port = 8080
server = HTTPServer(('', port), WebServerHandler)
print ("Web Server running on port %s" % port)
server.serve_forever()
except KeyboardInterrupt:
print (" ^C entered, stopping web server....")
server.socket.close()
if __name__ == '__main__':
main()
You can follow the link I provided above to read more from the documentation.
Solution 3
In python earlier than v3 you need to run http server as
python -m SimpleHTTPServer 8069
swissed
Updated on June 05, 2022Comments
-
swissed almost 2 years
Learning Python 2.7 and trying to run it on Vagrant.
Steps taken: Vagrant up Vagrant ssh Run command python webserver.py
Issue when I run this command, it gives out an ImportError: No module named BaseHTTPServer. Is this problem related to pg_config.sh? Thank you in advance for helping me understand.
I have checked my python 2.7 directory. BaseHTTPServer.py seems to be there.
from BaseHTTPServer import BaseHTTPRequestHandler, BaseHTTPServer class WebServerHandler(BaseHTTPRequestHandler): def do_Get(self): if self.path.endswith("/hello"): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_header() message = "" message += "<html><body>Hello!</body></html>" self.wfile.write(message) print message return else: self.send_error(404,'File Not Found: %s' % self.path) def main(): try: port = 8080 server = HTTPServer(('', port), WebServerHandler) print "Web Server running on port %s" % port server.serveforever() except KeyboardInterrupt: print " ^C entered, stopping web server...." server.socket.close() if _name_ == '__main__': main()