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).
Related
I tried to write a nested loop in a Python shell but got a syntax error after entering the second line:
for i in range(3):\
for j in range(3):\
SyntaxError: invalid syntax
To write for loops in the Python shell, you need to press Shift+Enter to insert a new line and remove the backslashes, because that will affect the syntax of the code that you are writing. Also, you will have to manually indent your code, adding the necessary spaces to make it work.
my subprocess.call problem is that my shortcut target is with extra INI file which is LIV2.INI and my exe file should run whit it . and my target link in shortcut looks like this
"C:\Program Files (x86)\AMO\EXE\PROGRAM LIVE 2.exe" LIV2.INI
i tried this
subprocess.call('"C:\Users\admin\Desktop\PROGRAM LIVE 2.exe" LIV2.INI')
and i tried this
subprocess.call('C:\Users\admin\Desktop\PROGRAM LIVE 2.exe LIV2.INI')
and i still get error that the ini file missing ? How can i fix this :)
THank you in advance
ERROR : INI FILE Missing or Wrong Name
Please also edit your question to actually include the error since you will get a syntax error, not an error that the ini file is missing.
You have two issues here, first you have a syntax error since "\Us" is not a valid string in python. \u marks the start of a Unicode escape sequence and the character S is not a valid unicode escape key. You can fix this by using double \\ to escape the \ character and tell python you want your string to include a \ and not use it as the start of a escape sequence.
Secondly, subprocess.call excpects a list, not a string (unless you set shell=True; but don't do that, since it means you have to manually escape things which you have already discovered is hard). The first element of the list
is the executable to run and the rest are command line arguments. For example if you wanted to run pythoneand print "hello world" you would type:
subprocess.call(['python', '-c', 'print ("hello world")'])
Notice the missing quotes around the python string? You don't need those since the command line arguments are passed in raw and no shell will attempt to split them if you don't include quotes.
Putting it all together creates something like this:
subprocess.call(['C:\\Users\\admin\\Desktop\\PROGRAM LIVE 2.exe', 'LIV2.INI'])
Notice the double backslashes and how each command line argument is its own list element.
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.
This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 9 years ago.
Note: This was not answered by the question that was marked as the original. This is more than just a Python v2 vs v3 problem, which I explain in the comments below.
Original post:
I am trying to learn Python at work, so I am currently using Portable Python 3.2.1.1 (which will henceforth be referred to as PP). (I mention this because this problem doesn't happen at home when I use my Mac and regular Python.)
I am working through exercise 16 of Learning Python the Hard Way (http://learnpythonthehardway.org/book/ex16.html). I've heard this isn't the best learning tool, but I am a complete programming n00b and I'm a hands-on learner. If you have any better suggestions, I'm open!
The first few lines of the exercise read:
from sys import argv
script, filename = argv
print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C (^C)."
My script is titled Ex16.py and the file I am using is Python.txt, and both of these are in the same folder as the PP .exes. I don't think that's necessary, but hoped maybe it would fix the problem... negative. When I press "Run" in PP, it doesn't work because argv requires you provide an argument when you start the script: python Ex16.py Python.txt
When I launch Python.exe (which, in PP is Portable-Python.exe), I get the standard Python prompt, >>>, but whatever I enter I get the same error message:
File "<stdin>", line 1
with whatever I've just tried repeated back to me with the marker to
indicate where the problem is. (has not been helpful so far)
SyntaxError: invalid syntax
I have tried typing the following at the >>> prompt:
python Ex16.py Python.txt,,
Ex16.py Python.txt,,
"%PATH&\Ex16.py" "%PATH%\Python.txt" (with the actual filepaths),,
print 'hello world'
I just keep getting the same invalid syntax error over and over. Even a basic print command returned an invalid syntax error. The only one that triggered a different error was the one where I tried whole filepaths. That one returned:
File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in
position 2-3: truncated \UXXXXXXXX escape
Yes, I have Googled the crap outta both errors. I read that sometimes the problem is not doubling the backspaces, so I tried that, too, putting two \ where just one had been before in both filepaths. I even tried putting — # -*- coding: utf-8 -*- at the beginning of the script thinking maybe there was some unicode error. That, with the full filepaths, resulted in the same unicode error mentioned earlier.
Yes, I have checked that my code is matching that in the exercise.
Yes, this works at home on non-PP.
All this leads me to believe that the problem is probably in the way I'm trying to run the scripts in PP (but why won't print work?), but I haven't a clue what I'm doing wrong.
Thanks!
print is a function in Python 3:
print('my string with content and the like')
It is no longer supported as being a 'statement'. You might want to check out a list of things that changed from python2.x to python3.x (there's a number of incompatibilities). Also, you might be better off finding a tutorial using Python3.
You have to type:
Portable-Python.exe Ex16.py Python.txt
at your command prompt. To get a command prompt, press WindowsKey-R, then type "cmd" and press enter. You should now be looking at something like c:\>. Navigate to your portable python installation by using the cd command.
I'm new to Python and it just confounds me with Indentation errors. In the following images, why does the first one work and the second one give me an indentation error?
Works:
Doesn't work: (Notice extra tree-expander that pops up in Notepad++)
Error:
File ".\sigma.py", line 14
for val in vs:
^
IndentationError: unexpected indent
I'm using Notepad++ and there are no spaces/tabs issues anywhere. Also, tried it out on the Python console typing it in exactly the same way in the 2nd image. It works fine. I'm guessing there's a very logical explanation to this, but coming from a strong-typed background (>5 years in Java), this feels like an unnecessary error.
You are mixing tabs and spaces. Don't do this, it creates inconsistent indentation problems.
Run your script through the tab checker:
python -tt script.py
and fix any and all tabs (replace with spaces), then configure your editor to only use spaces.
For Notepad++, see:
Convert tabs to spaces in Notepad++
How does one configure Notepad++ to use spaces instead of tabs?