I'm getting a SyntaxError in Python 3 - python-3.x

I am a beginner in Python. I am using the latest version of Python 3.2 on Windows 7. I found that print, count, raw_input("") and many others are showing errors. Here's an example of the error:
>>> print "Any body to help"
File "<stdin>", line 1
print "Any body to help"
^
SyntaxError: invalid syntax

You're reading Python 2.x material. Find Python 3.x material and read that instead. The language has changed.

Yes, the syntax was changed. Seeing as a lot of the code out there still is not 3.x, I'm guessing you may or may not actually be able to find the equivalent code to what you're looking at in 3.x. So to answer your specific question though you should type prints as follows in python 3.x.
print("Any body to help")
Hope this helps.
Edit: This should help clear up your raw_input as well. Another SO Question

Related

Python 3.10 antlr parser

I have been using an antlr v4 grammar to parse code for my UML diagrammer. I would like to just replace my current 3.6 .g4 file with a 3.10 one. My current grammar does not support the new Python match statement.
I found the following but it generates Java code. I need one that generates Python code.
Does anyone know if such a thing exists?

Declaring a constant variable in Python 3.8 triggers "Module 'typing' has no attribute 'Final'" error/warning (Python 3.8, Thonny IDE)

I'm trying to declare constants in Python 3.8 with the following code:
from typing import Final
It is a new feture in Python 3.8. See https://docs.python.org/3/library/typing.html for details.
Thonny IDE's bundled interpreter (3.7) gives an error:
Traceback (most recent call last):
File ".../decimal_arithmetics.py", line 2, in
from typing import Final
ImportError: cannot import name 'Final' from 'typing'
(/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.7/lib/python3.7/typing.py)
It is understandable, while it is a new feature in version 3.8 and Thonny still uses version 3.7.
However, with the interpreter changed in preferences to a separately installed Python 3.8 one, I still have an IDE warning:
Line 2 : Module 'typing' has no attribute 'Final'
What is the cause of this warning?
Please, let me know if some other information is necessary to clarify the question.
Explanation
Thonny's assistant uses Pylint and Mypy in order to provide the warnings. Pylint is mostly used for checking if the code is using proper Python coding standards (PEP8), while Mypy is used for checking static types for Python. In this case, the warning "Module 'typing' has no attribute 'Final'" is coming from Mypy.
Now, why does Mypy show that warning when Final does exist within typing on your interpreter? It's showing the warning because it's not checking your interpreter's typing.py, it's checking Thonny interpreter's typing.py. It doesn't matter that you've set it to use your own interpreter over Thonny's, the assistant always checks your code as if you were using Thonny's interpreter.
Solution
There are a couple ways you could stop the warning from appearing:
you could replace Thonny interpreter's typing.py with your interpreter's typing.py, but this is risky as it may lead to unexpected behavior if you ever decide to use Thonny's interpreter;
you could turn off Mypy checks for the assistant. You can do this by going to: Tools->Options...->Assistant and then uncheck Perform MyPy checks.

I cannot start any program from PythonStdioGames and get an error, how can I prevent this?

I am relatively new to Python and programming in general and currently learning Python with the book "Automate the boring stuff with Python" by Al Sweigart. As recommended in the book, I wanted to do some programming exercises myself by looking into the module gamesbyexample (https://github.com/asweigart/pythonstdiogames/).
However, whenever I start one of the games I get the following error message in the Terminal:
AdminisatorsMBP:~ simon$ /Library/Frameworks/Python.framework/Versions/3.8/bin/python3 /Users/simon/Library/Python/3.8/lib/python/site-packages/gamesbyexample/__crashdetector__.py 0.1.5 /Users/simon/Library/Python/3.8/lib/python/site-packages/gamesbyexample/alphabetizequiz.py
File "/Users/simon/Library/Python/3.8/lib/python/site-packages/gamesbyexample/__crashdetector__.py", line 16
except KeyboardInterrupt, EOFError:
^
SyntaxError: invalid syntax
I tried using multiple versions of python, 3.7.7 and 3.8.2, but with both versions, the same message comes up.
How could I get this running properly?
This is a syntax error, which means that you're probably missing something in the code that the compiler was expecting to be there.
If you could please also post the code, we could see what the problem is.

Python 3 backward compatibility for print statement

I just installed a python based library that claims to be compatible with python 2.7+. However, when I go to the run the first sample in their readme I get the SyntaxError: Missing parentheses in call to 'print'.
Groan!
I suppose I can dial code back to use Python 2.7. But before I go there, I wonder if there might exist some other widget that can be installed that perhaps the developers had access to that automatically allows the unparenthesized print statement to work without recoding the whole mess?

I'm getting an invalid syntax error in configparser.py

I'm trying to get the pymysql module working with python3 on a Macintosh. Note that I am a beginning python user who decided to switch from ruby and am trying to build a simple (sigh) database project to drive my learning python.
In a simple (I thought) test program, I am getting a syntax error in confiparser.py (which is used by the pymysql module)
def __init__(self, defaults=None, dict_type=_default_dict,
allow_no_value=False, *, delimiters=('=', ':'),
comment_prefixes=('#', ';'), inline_comment_prefixes=None,
strict=True, empty_lines_in_values=True,
default_section=DEFAULTSECT,
interpolation=_UNSET):
According to Komodo, the error is on the second line. I assume it is related to the asterix but regardless, I don't know why there would be a problem like this with a standard Python module.
Anyone seen this before?
You're most certainly running the code with a 2.x interpreter. I wonder why it even tries to import 3.x libraries, perhaps the answer lies in your installation process - but that's a different question. Anyway, this (before any other imports)
import sys
print(sys.version)
should show which Python version is actually run, as Komodo Edit may be choosing the wrong executable for whatever reason. Alternatively, leave out the parens and it simply fails if run with Python 3.
In Python 3.2 the configparser module does indeed look that way. Importing it works fine from Python 3.2, but not from Python 2.
Am I right in guessing you get the error when you try to run your module with Komodo? Then you just have configured the wrong Python executable.

Resources