[python] 'Syntax Error: invalid syntax' for no apparent reason

I've been trying to get a fix and can't find why the error keeps appearing. Pmin,Pmax,w,fi1 and fi2 have all been assigned finite values

guess=Pmin+(Pmax-Pmin)*((1-w**2)*fi1+(w**2)*fi2)

When i remove this line from the code, the same error appears at the next line of code, again for no reason I can think of

Edit: Here is the chunk of code I was referring to:

def Psat(self, T):
    pop= self.getPborder(T)
    boolean=int(pop[0])

    P1=pop[1]
    P2=pop[2]
    if boolean:
        Pmin = float(min([P1, P2]))
        Pmax = float(max([P1, P2]))
        Tr=T/self.typeMolecule.Tc
        w=0.5*(1+scipy.tanh((10**5)*(Tr-0.6)))
        fi1=0.5*(1-scipy.tanh(8*((Tr**0.4)-1)))
        fi2=0.460*scipy.sqrt(1-(Tr-0.566)**2/(0.434**2)+0.494

        guess = Pmin+(Pmax-Pmin)*((1-w**2)*fi1+(w**2)*fi2)   #error here

        solution = scipy.optimize.newton(funcPsat,guess, args=(T,self))

This question is related to python syntax-error spyder

The answer is


For problems where it seems to be an error on a line you think is correct, you can often remove/comment the line where the error appears to be and, if the error moves to the next line, there are two possibilities.

Either both lines have a problem or the previous line has a problem which is being carried forward. The most likely case is the second option (even more so if you remove another line and it moves again).

For example, the following Python program twisty_passages.py:

xyzzy = (1 +
plugh = 7

generates the error:

  File "twisty_passages.py", line 2
    plugh = 7
          ^
SyntaxError: invalid syntax

despite the problem clearly being on line 1.


In your particular case, that is the problem. The parentheses in the line before your error line is unmatched, as per the following snippet:

# open parentheses: 1  2             3
#                   v  v             v
fi2=0.460*scipy.sqrt(1-(Tr-0.566)**2/(0.434**2)+0.494
#                               ^             ^
# close parentheses:            1             2

Depending on what you're trying to achieve, the solution may be as simple as just adding another closing parenthesis at the end, to close off the sqrt function.

I can't say for certain since I don't recognise the expression off the top of my head. Hardly surprising if (assuming PSAT is the enzyme, and the use of the typeMolecule identifier) it's to do with molecular biology - I seem to recall failing Biology consistently in my youth :-)


You're missing a close paren in this line:

fi2=0.460*scipy.sqrt(1-(Tr-0.566)**2/(0.434**2)+0.494

There are three ( and only two ).
I hope This will help you.


If you are running the program with python, try running it with python3.


I encountered a similar problem, with a syntax error that I knew should not be a syntax error. In my case it turned out that a Python 2 interpreter was trying to run Python 3 code, or vice versa; I think that my shell had a PYTHONPATH with a mixture of Python 2 and Python 3.


I noticed that invalid syntax error for no apparent reason can be caused by using space in:

print(f'{something something}')

Python IDLE seems to jump and highlight a part of the first line for some reason (even if the first line happens to be a comment), which is misleading.


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to syntax-error

Uncaught SyntaxError: Unexpected token u in JSON at position 0 How to solve SyntaxError on autogenerated manage.py? Checking whether the pip is installed? (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape How can you print multiple variables inside a string using printf? Unexpected token < in first line of HTML Laravel: Error [PDOException]: Could not Find Driver in PostgreSQL 'Syntax Error: invalid syntax' for no apparent reason How can I fix MySQL error #1064? Notice: Trying to get property of non-object error

Examples related to spyder

Anaconda Navigator won't launch (windows 10) How to start Spyder IDE on Windows How to change python version in anaconda spyder Python Pandas - Missing required dependencies ['numpy'] 1 how to update spyder on anaconda How to change the Spyder editor background to dark? Shortcut key for commenting out lines of Python code in Spyder ImportError: No module named 'google' %matplotlib line magic causes SyntaxError in Python script How to run Spyder in virtual environment?