2016-09-18 55 views
0

我试图将我的项目转换为python3。不能“导入urllib.request,urllib.parse,urllib.error”

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

我该怎么做?

+0

你确定你想在Python 3运行的代码,而不是Python的2? –

+0

只需检查 - “python3 -V”显示什么? –

+0

对不起。我犯了一个错误。我运行'python3 ./server.py',但我在使用python2的编辑器上测试过。 – Bonn

回答

1

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

简单地改变第一行:

#!/usr/bin/env python3 
+0

已经工作!非常感谢大家^。^ – Bonn