Basic Python Hello World Program Syntax Error

Basic Python Hello World Program Syntax Error

Check to make sure that you are really using straight quotes. Some programs/systems automatically replace them with curly quotes.

Here try copy pasting this and running it:

print "hello"

If this turn's out to be an issue then you might want to ask at https://unix.stackexchange.com/ how to disable such a feature.

print(hello world, end=' ') causes syntax error

I am running python 2.7, and have imported print_function in the program that calls my module

That doesn't work. You have to do the future statement in this module, not in the script or module that imports it.

See the documentation for details. But the short version is: The future statement changes the way your module is compiled, so Python has to be able to see it at module compilation time, not just at runtime. (That's also why it has to be the first non-comment/docstring line in the file.)


OK, so that explains how to fix it, but it doesn't explain why there's a problem unless you already know when module compilation happens.

Oversimplifying a bit: When you import spam (if it hasn't already been imported during this session), Python goes looking for an appropriate spam.py file. If it finds one, it then looks for a spam.pyc that's newer than spam.py. If so, it just executes that. If not, it compiles spam.py into spam.pyc, then executes it.

So, because future statements affect the way code is compiled, if Python wanted to let your future statements affect other modules that you imported, it would have to store a different version of spam.pyc for each possible combination of future statements—maybe spam.pyc, spam+print_function.pyc, spam+division.pyc, spam+print_function+division.pyc, etc.


Finally:

How can I fix this without importing print_function twice?

You really can't. Well, you can, but you don't want to. The cleanest and simplest way I can imagine doing this is to write an import hook that overrides the usual mechanism for looking for and compiling .pyc files to ensure that everything you import is treated as if it had all of your future statements applied (possibly using something like the spam+print_function.pyc trick I semi-facetiously suggested above, to make sure you don't collide with normal cache .pyc files. (See the compile docs for info on letting the modules inherit your future flags, or specifying a set of them explicitly.)

If that sounds really cool to you, learning about Python's import system is a lot of fun, but I would strongly suggest waiting until you upgrade to at least Python 3.4 before doing so, because it's changed a lot, and for the better; it's a lot easier to learn (especially since the whole thing is written in Python and thoroughly documented, instead of a mess of C code scattered around three different places), and a lot more flexible (so you usually don't have to duplicate half of what Python normally does for you, you can just replace the one part you want to change).

Understanding Python syntax errors

The reason you're seeing different outputs with the two exceptions is because one is a run-time exception and the other is a parsing exception.

Before Python can run your script, it has to parse your code and translate it into Python bytecode. Python parses you're code by verifying it has valid syntax. If Python finds your script contains invalid syntax, it raises a SyntaxError and stops. The important point to note here is that no code has been run. As I stated, Python's parsing of a script occurs before it attempts to run the script. That's why in your second example, the first line is not run. Python's still in the stage of parsing your script.

In your first example, however, Python has already parsed you're code and verified it has valid syntax. Now Python attempts to execute your code. The Python interpreter will continue to run until it encounters an error, at which point it stops. The important point to note here is that Python is in the execution stage when it raises a NameError. As I said above, Python will execute code in a script as long as it can. That's why in your first example, the first statement is run, and then the Python interpreter stops.

Basically, SyntaxError and NameError are two different exception types. SyntaxError's are raised during Python's parsing stage, while NameErrors are raised during Python's execution (a.k.a run-time) stage. The output the exceptions produce is different because the exceptions are raised during different stages and for different purposes.

Syntax error near unexpected token ` Hello World! ' on Ubuntu

This error comes from bash, not Python. As if you were to type print("Hello World") in your terminal. Try running the python file as python3 file3.py.

What is the SyntaxError here in return 'h1Hello world/h1' when I try to launch my Flask application?

index is a function, needs parenthesis

from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return '<h1>Hello world</h1>'

Getting a Syntax Error in python with first program

print(len(myName)
Missing ')'.

Syntax error on hello world with python

print() is for python 3.x, to make it work in python 2.x you need to import it first:

In [3]: from __future__ import print_function

In [4]: print("Hello","World", sep="***")
Hello***World


Related Topics



Leave a reply



Submit