Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I am importing `http.server` from the idle it works, but when I run a python file having `import http.server` there is an error

When I Am Using:

>>> import http.server

in the IDLE there isn't any error.
But When I am Using This Piece Of Code:

import http.server
from http.server import BaseHTTPRequestHandler
from http.server import HTTPServer

def run(server_class = HTTPServer, handler_class = BaseHTTPRequestHandler):
    server_address = ('', 8000)
    httpd=server_class(server_address, handler_class)
    httpd.serve_forever()


run()

There Is An Error Which Is As Follows:

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 2195, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/toton/Projects/http.py", line 1, in <module>
    import http.server
  File "/home/toton/Projects/http.py", line 1, in <module>
    import http.server
ImportError: No module named 'http.server'; 'http' is not a package

Please Help!

like image 971
Ikari Avatar asked Feb 21 '15 13:02

Ikari


1 Answers

You have named your file as http.py, Thus it is overriding the original module http

To solve

  • change the name of the file to something else
  • Remove the pyc file
  • Run the program again
like image 169
Bhargav Rao Avatar answered Nov 04 '22 20:11

Bhargav Rao