I'm taking google's python tutorial and may have fat fingered a few keys, causing an error. I don't recognize the issue here, and the ctrl+click links it allows me to follow take me to line 1 of the file I'm writing in and to python.exe.
It looks like there's an extra character somewhere in a file path? There are no syntax errors in the code itself as the debugger runs through it just fine.
I'm using Visual Studio Code
None of the code I've written (with my knowledge) is causing this error.
This is the error message I'm getting.
SyntaxError: invalid syntax
>>> & C:/Users/mcgilm1/AppData/Local/Programs/Python/Python37-32/python.exe c:/Users/mcgilm1/Documents/google-python-exercises/basic/string2.py
File "<stdin>", line 1
& C:/Users/mcgilm1/AppData/Local/Programs/Python/Python37-32/python.exe c:/Users/mcgilm1/Documents/google-python-exercises/basic/string2.py
^
Including your code could help us, here is a list of things to check that I found on the web: Debugging
Make sure you are not using a Python keyword for a variable name.
Check that you have a colon at the end of the header of every compound statement, including for, while, if, and def statements.
Check that indentation is consistent. You may indent with either spaces or tabs but it’s best not to mix them. Each level should be nested the same amount.
Make sure that any strings in the code have matching quotation marks.
If you have multiline strings with triple quotes (single or double), make sure you have terminated the string properly. An unterminated string may cause an invalid token error at the end of your program, or it may treat the following part of the program as a string until it comes to the next string. In the second case, it might not produce an error message at all!
An unclosed bracket – (, {, or [ – makes Python continue with the next line as part of the current statement. Generally, an error occurs almost immediately in the next line.
Check for the classic = instead of == inside a conditional.
Related
I recently encountered the common "unexpected indent" problem when trying to evaluate python code by copying them from PyDev and Emacs into a python interpreter.
After trying to fix tab/spaces and some searches, I found the cause in this answer:
This error can also occur when pasting something into the Python
interpreter (terminal/console).
Note that the interpreter interprets an empty line as the end of an
expression, so if you paste in something like
def my_function():
x = 3
y = 7
the interpreter will interpret the empty line before y = 7 as the end
of the expression ...
, which is exactly the case in my situation. And there is also a comment to the answer which points out a solution:
key being that blank lines within the function definition are fine,
but they still must have the initial whitespace since Python
interprets any blank line as the end of the function
But the solution is impractical as I have many empty lines that are problematic for the interpreter. My question is:
Is there a method/tool to automatically insert the right number of initial whitespaces to empty lines so that I can copy-and-paste my code from an editor to an interpreter?
Don't bother with inserting spaces. Tell the interpreter to execute a block of text instead:
>>> exec(r'''
<paste your code>
''')
The r''' ... ''' tripple-quoted string preserves escapes and newlines. Sometimes (though in my experience, rarely) you need to use r""" ... """ instead, when the code block contains tripple-quoted strings using single quotes.
Another option is to switch to using IPython to do your day-to-day testing of pasted code, which handles pasted code with blank lines natively.
In some code I just added to a larger python file that already uses the syntax of Tabs instead of spaces (which I know is not recommended), I'm getting a syntax error in the code below. I'm using vim/python2.4, and turned on :set list to see whitespace characters. It doesn't look like I'm violating any indentation rules, and I'm following what exception should look like according to the documentation/other parts of the code which are working properly.
def writeXmlFile(self, testFilekey):
#dictionary for xml values
xml_d={}
try:
xml_d['test_r']=self.test_results
except: TypeError
xml_d['test_r']=-1 <-Syntax error at the first non-whitespace (x of xml_d)
print "test_results"
print xml_d['test_r']
Does this have to do with whitespace, or is there something else that I'm overlooking completely here?
Posting code like this is not helpful; it's very hard to see the actual text.
The problem is not with indentation, but with syntax, like the error says. The colon goes after the exception class, not before:
except TypeError:
All indented blocks in Python are introduced with a colon on the end of the previous line.
Im using the terminal on my mac to run some python and when i try to print a string i get an invalid syntax error.
Michaels-MBP:~ mike$ python text.py
File "text.py", line 2
print(‘hi’)
^
SyntaxError: invalid syntax
I've tried it with single quotes and with and without parentheses but i keep getting that error, what is wrong.
Should be:
print('hi')
You have proper British quotes ‘foo’. Those are the right symbols to use when writing human-readable texts, but Python wants actual single quotes '.
Your editor probably has some kind of smart-quotes feature enabled, it is wise to turn this off when writing code (e.g. configure your editor to detect extensions like .py).
For some reason at the end of of my python file in IDLE I'm getting an indentation error, even though the block of code has the right amount of spacing. Also when I try to run and am told about the error, it actually highlights the whole line after the code, instead of highlighting the character before a line of wrongly indented code, as it normally does.
Here's a screenshot demonstrating where it highlights:
For reference, this is how looks when highlighting a clearly real indent problem:
The code itself has problems with this form:
if yes.lower() == 'y': linkFiles(folders)
raw_input(iprint('= Script Finished, press enter to quit.',True))
and this form:
if yes.lower() == 'y':
linkFiles(folders)
raw_input(iprint('= Script Finished, press enter to quit.',True))
Also yes, I have confirmed that I'm not mixing tabs and spaces. Just using spaces on all these lines.
I also checked about any whitespace at the end of the file, but this error occurs the same even when there's no characters whatsoever after the closing bracket. And attempting to run the script does add a newline character, but nothing else.
As per a suggestion, I did try to run it through the commandline and got essentially the same result:
C:\Windows\system32>python "C:\Users\ntreanor\Desktop\User Friendly Scripts\Create file links.py"
File "C:\Users\ntreanor\Desktop\User Friendly Scripts\Create file links.py", line 135
^
IndentationError: unexpected unindent
This was actually way more simple than I had thought.
I was writing a try: except: block over the whole script, basically because I'm writing to a log and I want it to spit out any exceptions I get into the log so that I can troubleshoot after the fact when people have used my script. The details of this are from another question here
The problem was I got interrupted when writing the script and forgot I had added the try but not the except. So the actual error was that I was missing the whole except block. Once I even just added
except: pass
at the end, it ran fine.
got a bit of a noob question.
I'm trying to get Metagoofil working because it keeps saying "error downloading webpage" etc etc.
A google search found that I can change a bit of the code in one of the config files and it will work properly again.
I'm having a problem though: this seems to be the code I want to use.
self.url = url.replace("/url?q=", "", 1).split("&")[0]
BUT, it doesn't seem to like me (based on syntax highlighting) have those two quotation marks together with nothing in between. When they are like above, it starts highlighting .split(" up to here thinking that this is the string.
My question is, how can I make the double quotations together without anything in the middle and have it register as its own string, so it doesn't highlight the .split("
The literal "" is a valid string with a length of zero. I guess your syntax highlighter is not working properly.