0

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?

TuringTux
  • 559
  • 1
  • 12
  • 26
Bonn
  • 183
  • 14
  • Are you sure you're trying to run that code in Python 3 and not Python 2? – Jon Clements Sep 18 '16 at 15:35
  • Just to check - what does `python3 -V` show? – Jon Clements Sep 18 '16 at 15:41
  • I'm sorry. I have made a mistake. I run 'python3 ./server.py', but I tested on editor that use python2. – Bonn Sep 18 '16 at 15:43
  • However ploblem still going on python3 server: 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 – Bonn Sep 18 '16 at 15:48
  • So complicate! I run python3 ./server.py. But when I using python3 syntax in cgi script 'print(sys.version)', I got '2.7.12 (default, Jul 1 2016, 15:12:24) [GCC 5.4.0 20160609]' ?!?!? – Bonn Sep 18 '16 at 15:59
  • We're missing the context of how cgi is involved here... Apart from an indication it's involved in the error message.. Are you creating a cgi script that's being served via Apache or similar - that may have an older embedded Python (or otherwise be picking up an older Python)? – Jon Clements Sep 18 '16 at 16:01
  • I have added code in server.py to above question. thank you. – Bonn Sep 18 '16 at 16:08
  • Possible duplicate of [Python3 error: "Import error: No module name urllib2"](http://stackoverflow.com/questions/2792650/python3-error-import-error-no-module-name-urllib2) – cs01 Sep 18 '16 at 16:13

1 Answers1

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
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93