Can't “import urllib.request, urllib.parse, urllib.error”

百般思念 提交于 2019-12-25 08:29:49

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/39559384/cant-import-urllib-request-urllib-parse-urllib-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!