2016-09-18 52 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