Invalid syntax in lapack testing file - python

When i compiled the LAPACK-3.4.2 with MinGW,i got a error like this:
File "./lapack_testing.py",line 17
**except getopt.error, msg:
^**
syntaxError:invalid syntax
Make:***[lapack_testing] Error 1
But i checked the lapack_testing.py,and there was no problem with the syntax.
Any idea?Thanks!

That exception handling syntax is valid in Python 2. Starting with Python 3 that syntax is no longer valid. Seep PEP 3110.
So it looks like the code you are running is designed for Python 2 but you are trying to execute it on Python 3. The expedient solution is going to be to run the code under Python 2, as it is intended to run.

Related

How do I fix this syntax error with print in Python?

I copied the Python code that Google Cloud recommends. I've tried it in Jupyter Notebook and as a Python file but I still get the syntax error:
def predict(content, project_id, model_id):
# More code.
pass
print predict("1.jpg", ispace-254720, ICN886814670746214838)
Gives:
File "<ipython-input-16-a9c7141e2bf3>", line 1
print predict("1.jpg", ispace-254720, ICN886814670746214838)
^
SyntaxError: invalid syntax
It's probably old Python 2.7 code. I'm guessing you're using Python 3, in which case print is now a function, so you need to put parentheses after print:
print(predict("1.jpg", ispace-254720, ICN886814670746214838))

Syntax error only in python 3

I have the following code which runs fine on python 2 but throws error on python 3
import sys
if sys.version_info > (3,):
#print("Python 3")
#Try Block
except urllib2.HTTPError as err:
else:
#print "Python 2" # Throws error on python 3
#Try Block
except urllib2.HTTPError, err: # Throws error on python 3.
The above code returns "Python 2" in python 2, but throws syntax error on python 3 (For python 2 syntax ).
Can anyone tell me why is this happening ? And what is the work around to fix those syntax errors in python 3 ?
Note : I am aware of print syntax on Python 3
Thanks you!
The problem is that the parser runs before any code is evaluated at runtime. Your check sys.version_info > (3,) runs at run time, after the code was already parsed and compiled. So doing such checks, you are able to make changes at runtime, but that does not help you when dealing with syntax changes.
The syntax is parsed and compiled before any code is interpreted, that is why you get syntax errors even for code that is never run at runtime.
If you are trying to create a polyglot script that is able to run on both Python 3 and Python 2, then you will need to make sure to use a syntax that works on both. For print in particular, you can import the print function in Python 2, so you can use it just like you would in Python 3:
from __future__ import print_function
Some newer features won’t work that way (for example everything async), but for the most part, you can make it work somehow.
If you end up depending on stuff with Python 3 that requires Python 2-incompatible syntax, then you could put that into a separate module and import that module conditionally at runtime. That way it won’t be loaded for Python 2, so the Python 2 parser wouldn’t attempt to load the incompatible syntax.
As for why Python 2 does not throw an error, that’s actually very simple: print('foo bar') is valid syntax in Python 2, even with the print statement. That is because you can put parentheses around anything without impacting the value. So what you actually do there is this:
print ('foo bar')
^^^^^
print statement
^^^^^^^^^^^
value, wrapped in parentheses (that don’t do anything)
That’s also the reason, why the following produces different results in Python 3 and 2:
print('foo', 'bar')
In Python 3, you get foo bar as the output, while Python 2 gives you ('foo', 'bar'). That is because the comma inside the parentheses now makes this a tuple, so you pass a tuple to the print statement. – Importing the print function fixes this to give you the same behavior on Python 2 as on Python 3.
Even though the line print "Python 2" will never be executed in Python 3, it will still be compiled to byte code (or at least attempted). That line is a syntax error in Python 3, which requires the printed items to be in parentheses. Look up the documentation for more details: other changes to print were also made.
So remove the error. Make the line
print("Python 2") # Throws error on python 3
instead. That way it works in both Python 2 and in 3. There are many sites that discuss how to write code that executes in both versions of Python. The parentheses are merely ignored in version 2 but are needed in version 3. This works if you print only one item: more than one gets more complicated.
There is another way to do printing in both versions, using from __future__ import print_function but the way I showed is easier.
Note that your attempted comment in the line is also not proper Python syntax. Change the // to # and it works.

Cant make simple Python program run through Windows command prompt

So I wrote a very simple python test file called test testProg.py, and it looks like this:
import sys
def adder(a, b):
sum = a+b
print sum
if __name__ == "__main__":
a = int(sys.argv[1])
b = int(sys.argv[2])
adder(a, b)
From another question here, I did the command:
python testProg.py 3 4
However I get the following error message:
File "testProg.py", line 5
print sum
^
SyntaxError: invalid syntax
I am honestly not sure what the issue it... I can run python from the command prompt easily with no issue, but why cant I replicate that questions' solution?
Thanks.
Edit: Python 3.4 is used
It looks like you have Python 3 installed. The code you are running was written for Python 2, which has slightly different syntax. For this example, you need to change that to print(sum). In general, you should search around for information on the difference between Python 2 and 3, and be careful to note what version is used in code you find on the internet. Code written for Python 2 will often not run as-is on Python 3.

Code changes from Python 2.6 to 3.x

I am trying to get pywbem working in Python 3.2 (it works fine in 2.6) but the build fails on this part of code in mof_compiler.py:
File "pywbem-0.7.0\mof_compiler.py", line 1341
print s
^
SyntaxError: invalid syntax
It's a macro, defined like this:
def _print_logger(s):
print s
I don't understand why this is invalid, please explain how to do the same in Python 3.2.
Note: I have little or no experience with Python.
PS: I have already done some small changes to the code for 3.2 like
changing
except CIMError, ce:
to
except CIMError as ce:
based on Lennart Regebro's answer here are some other changes I found (placing them here since it may be useful for others).
exec "import %s as lextab" % tabfile -> exec ("import %s as lextab" % tabfile)
raise ValueError,"Expected a string" -> raise ValueError("Expected a string")
That's not a macro, it's a function definition, and in Python 3 the print statement is now a function. So do print(s) instead.
The list of changes between Python 2 and Python 3 is here: http://docs.python.org/release/3.0.1/whatsnew/3.0.html
It's not so easy to read, but I don't know if there is a better one online (although books exist).
If you are going to use Python 3, you would probably do good to get a Python 3 book. There are a couple of them out now. Or at least refer to the Python 3 documentation: http://docs.python.org/release/3.2/ It has a decent tutorial.
One of the most visible changes in python 3 is print is no longer a statement, but is a function, so you have to use parenthesis for calling that function. print(s)
Also, if you have your Python2 code, just use 2to3 which can do a source to source translation of your python2 to python3, which can fix most of the syntax level changes for you like the above problems. 2to3 is installed with python3 binary.
Sorry for answering an old question, but I just recently wanted to get PyWBEM running under Python 3, so I forked it, made the required changes, and removed a Python 2.x dependency (M2Crypto) from it for the 3.x series. Here's the source from GitHub:
https://github.com/deejross/python3-pywbem
Quick note, this supports Python 2.6, 2.7, and 3.4+

Python 2.6 DB error

I'm trying to get the Yahoo! BOSS package working, but when I try to run the example file I get the following error:
$ python examples/ex5.py
File "examples/ex5.py", line 28
tb = db.group(by=["yn$title"], key="rank", reducer=lambda d1,d2: d1+d2, as="total", table=tb, norm=text.norm)
^
SyntaxError: invalid syntax
This particular error only occurs in Python 2.6. When I attempt to run in Python 2.5, this error does not occur (but it throws other errors because I was haven't installed a number of other supporting packages in 2.5).
Note that all the example files fail on this db.group function, with the carat highlighting "as" in each case.
Is there a 2.6-related change that could be causing this error?
as was a pseudo-keyword in 2.5, it's become a full-fledged keyword in 2.6 -- that's definitely the cause of your problem!
As for the workaround, try adding a **{'as': 'total'} at the end of your call and remove the plain as='total' -- that should work.

Categories

Resources