BallWarapol BallWarapol - 2 months ago 15
Python Question

Can't "import urllib.request, urllib.parse, urllib.error"

I trying to convert my project to python3.

My server script is server.py:

#!/usr/bin/env python
#-*-coding:utf8-*-

import http.server
import os, sys
server = http.server.HTTPServer
handler = http.server.CGIHTTPRequestHandler
server_address = ("", 8080)
#handler.cgi_directories = [""]
httpd = server(server_address, handler)
httpd.serve_forever()


But when I try:

import urllib.request, urllib.parse, urllib.error


I get this in terminal of python3 ./server.py:

import urllib.request, urllib.parse, urllib.error
ImportError: No module named request
127.0.0.1 - - [18/Sep/2016 22:47:18] CGI script exit status 0x100


How can I do this?

Answer

Your shebang suggests that code should be run by python binary, which is historically associated with Python 2.x releases.

Simply change first line to:

#!/usr/bin/env python3
Comments