不能"import urllib.request, urllib.parse, urllib.error"

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

我正在尝试将我的项目转换为 python3。

我的服务器脚本是 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()

但是当我尝试时:

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

我在 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

我该怎么做?

您的 shebang 建议代码应该是 运行 by python 二进制,这在历史上与 Python 2.x 版本相关联。

只需将第一行更改为:

#!/usr/bin/env python3