Syntax error with a simple Python import

Jan Hančič

My GF is trying to follow the Udacity's Web Development course but she ran into a problem. And I can't solve it. It's just at the start when one has to create a "hello world" Python script that runs on AppEngine.

So, the files:

app.yaml:

application: focus-invention-298
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: helloworld.app

helloworld.py:

# -*- coding: utf8 -*-

​import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, Udacity!')

application = webapp2.WSGIApplication([('/', MainPage)], debug=True)

But, when I run the app (either trough the GUI launcher or with the dev_appserver.py) and open the app in the browser I get this error (in the console):

Traceback (most recent call last):
  File "/Users/Kaja/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 196, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/Users/Kaja/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 255, in _LoadHandler
    handler = __import__(path[0])
  File "/Users/Kaja/Documents/udacity/helloworld.py", line 3
    ​import webapp2
    ^
SyntaxError: invalid syntax
INFO     2013-08-05 14:06:00,875 module.py:595] default: "GET / HTTP/1.1" 500 -
ERROR    2013-08-05 14:06:01,012 wsgi.py:219] 
Traceback (most recent call last):
  File "/Users/Kaja/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 196, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/Users/Kaja/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 255, in _LoadHandler
    handler = __import__(path[0])
  File "/Users/Kaja/Documents/udacity/helloworld.py", line 3
    ​import webapp2
    ^
SyntaxError: invalid syntax

We are on OSX 10.8.4 and when I run python in the terminal it tells me I have 2.7.2 version installed. AppEngine launcher (or SDK) version is 1.8.2.

Anyone? I've tried so many things now without success that I really don't know what to do anymore (I'm not a python dev) and I really wanna make this thing work so my GF can continue learning :)

Martijn Pieters

There are bytes before the import statement (unicode non-breaking whitespace characters are a prime candidate) that could cause this.

Check the first 50 bytes or so:

print repr(open('helloworld.py', 'rb').read(50))

If you see a sequence like '\xc2\xa0' then you have UTF-8 encoded non-breakable space characters in there, for example.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

module to parse a simple syntax in python

From Dev

Python - In and Not In List Syntax Error

From Dev

Error in python after 'import tensorflow': TypeError: __init__() got an unexpected keyword argument 'syntax'

From Dev

Syntax Error on dynamic import() using webpack and babel

From Dev

Syntax Error in Simple Case Statement

From Dev

Python CMD Syntax error

From Dev

Syntax error with simple R function (Error: unexpected '}' in "}")

From Dev

Simple Syntax Error

From Dev

Invalid Python Syntax Error

From Dev

django syntax error in simple view

From Dev

syntax error in python code

From Dev

Import related error, simple program

From Dev

Simple python syntax

From Dev

Unknown Syntax Error in Python

From Dev

Curious sql import syntax error

From Dev

Jinja2.5 Syntax error on import

From Dev

Syntax error using keyboard input in a simple python code

From Dev

Syntax error on simple while loop

From Dev

Python - if or if invalid syntax error

From Dev

SOAPUI: SImple Groovy script - syntax error on import statement?

From Dev

Python exception syntax error

From Dev

else: statement syntax error, simple Python

From Dev

SQL syntax error with python

From Dev

Simple python if/else syntax not working

From Dev

Python syntax error with "

From Dev

Python - invalid syntax error

From Dev

Simple logical syntax termcolor python

From Dev

ChromeOptions() import is not working in python selenium syntax error?

From Dev

Simple import is throwing an error; any ideas on why I'm not able to import whisper using python?