Why are these Python scripts giving syntax errors? [duplicate] - python

This question already has answers here:
Invalid syntax (SyntaxError) in except handler when using comma
(5 answers)
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed last month.
I am trying to install python on windows, and this is my first day on python. Install goes well on windows 7 x64. But almost all scripts fails. I am trying to install celery and running following command on celery folder.
python setup.py build
and it fails, following is an error
File "setup.py", line 40
except ImportError, exc:
^
SyntaxError: invalid syntax
also following fails, which is valid print command i think.
>>> print 'a'
File "<stdin>", line 1
print 'a'
^
SyntaxError: invalid syntax
I am sure i am missing something here. Any idea what makes it fail?
Edit:
Below is summary of tasks i had to go through to get python working, made notes for myself but putting it here as well if it can help anyone
Install python and celery
=========================
-celery does not work with python3, so install latest python2
-install windows install for python2
-add C:\python2X to %PATH%
-set python path for lib
set PYTHONPATH=%PYTHONPATH%;c:\python2x
-install setuptools
http://pypi.python.org/pypi/setuptools
for x64 install does not work use
python setup.py install
-then can use easy_install
-now just use easy_install to install everything

A likely cause is version incompatibility, as Vincent Savard pointed out. Python 3 is not backwards compatible with Python 2
if print 1 doesn't work, but print(1) does, then you are running python 3, which seems to be the case

for Python 3 the syntax has been changed so
Change from except exc, var to except exc as var.
viz ( http://docs.python.org/release/3.1.3/whatsnew/3.0.html )

except ImportError, exc:
should be
except ImportError as exc:

Yeah you're probably running python 3. Try print("hello world")
If that works then you're running python 3

Related

Cassandra 3.11.3 and cqlsh not support python 3.6 and 3.7? [duplicate]

This question already has answers here:
Invalid syntax (SyntaxError) in except handler when using comma
(5 answers)
Closed last month.
I have installed Python 3.6 or 3.7 with Cassandra 3.11.3.
But it does not supporte cqlsh, it only supports the Python 2.7 version.
This is the error message:
\apache-cassandra-3.11.3\bin\\cqlsh.py", line 146
except ImportError, e:
^
SyntaxError: invalid syntax
What may be the problem?
Change
except a, b:
to
except a as b:
Add parens to prints
Change:
except ImportError, e:
To:
except ImportError as ex:
or
except ImportError:
Cqlsh is written in Py2.7, so it'll not build on py3 wrapper env. Even if you change the exception line, it'll not compile the. For example, take this line:
File "/home/usr/.local/bin/cqlsh", line 212
print '\nWarning: Specified cqlshrc location `%s` does not exist. Using `%s` instead.\n' % (CONFIG_FILE, HISTORY_DIR)
^
SyntaxError: invalid syntax
Optional solutions:
Downgrade your python version using sudo-alternatives (check versions with update-alternatives --list python)
Correct the entire file (not viable)
Connect throug datastax or similar driver and query.
If you have the both version, set the Python2.7 as the main one. You can check the principal version using the command python -V. Then change using this command sudo ln -sf /usr/bin/python3.6 /usr/bin/python (use your path python 3x and 2x). Check if it was ok using python -V. Call the $cqlsh again.
Ps.: If necessary check the cqlsh file, if the header is correct. If it is #!/usr/bin/python3 fix to #!/usr/bin/python. You can use $find / -name cqlshto find the files.

Cannot install PySpatiaLite module ("SyntaxError: Missing parentheses in call to 'print' ")

When I run
python3 -m pip install pyspatialite
I get the following error:
Collecting pyspatialite
Using cached https://files.pythonhosted.org/packages/cc/2a/ffb126f3e8890ab0da951a83906e54528a13ce4b913303dea8bed904e160/pyspatialite-3.0.1-alpha-0.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-14jnmfoo/pyspatialite/setup.py", line 66
print "Is sphinx installed? If not, try 'sudo easy_install sphinx'."
^
SyntaxError: Missing parentheses in call to 'print'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-14jnmfoo/pyspatialite/
I don't understand the issue. Is there a syntax error in the module PySpatiaLite? What do I do about it?
I am using Python 3.5 and Linux Bash Shell in Windows 10. If there is any additional info needed, let me know in the comments and I will edit the question.
Seems like it is a known issue in python 3:
https://github.com/lokkju/pyspatialite/issues/27
print "Is sphinx installed? If not, try 'sudo easy_install sphinx'."
Seems that this library is written in Python 2.7 as they are using the Python 2 print statement. When pip3 runs the libraries setup.py the error you are receiving:
SyntaxError: Missing parentheses in call to 'print'
Is entirely expected as the correct Python 3 syntax would be:
print("Is sphinx installed? If not, try 'sudo easy_install sphinx'.")
You can either switch to Python 2.7 for writing code to interface with this, or reach out to the contributors for assistance. Looking at their documentation on PyPi (https://pypi.org/project/pyspatialite/) it looks like the project is still in Alpha and has not been updated with a new release since 2013. I wouldn't expect much in terms of Python 3 compatibility without forking the source and correcting it yourself.
EDIT
Looking at the GitHub commits (https://github.com/lokkju/pyspatialite/commits/master) a small amount of commits have been merged in since 2013, but I would still not expect Python 3 support.

Install requirements.txt error, syntax error - python2

I have this requirements.txt file:
Django==1.9.4
EbookLib==0.15
SpeechRecognition==3.3.3
argcomplete==1.1.0
argparse==1.2.1
beautifulsoup4==4.4.1
chardet==2.3.0
lxml==3.5.0
pdfminer==20140328
python-docx==0.8.5
python-pptx==0.5.8
requests==2.9.1
textract==1.4.0
wsgiref==0.1.2
xlrd==0.9.4
When I run $pip install -r requirements.txt in my virtualenv i have the following error:
Collecting pdfminer==20140328 (from -r requirements.txt (line 9))
Using cached pdfminer-20140328.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 20, in <module>
File "/tmp/pip-build-ymmlp5sc/pdfminer/setup.py", line 3, in <module>
from pdfminer import __version__
File "/tmp/pip-build-ymmlp5sc/pdfminer/pdfminer/__init__.py", line 5
print __version__
^
SyntaxError: Missing parentheses in call to 'print'
It's seems that this file is written in python2.
Is there a way to fix this and all of the requirements to be installed ?
EDIT:
If I try it to install in my global env, there is no problem. If I'm in virtualenv it first tries to collect something. It acts different ..
In python 3K, print is not a reserved keyword, but a built-in function, so you must use it like below:
print("Hello world!")
If you use print as a keyword, the interpreter will raise an exception:
SyntaxError: Missing parentheses in call to 'print'
You encounter such problem for installing a library, namely, pdfminer here, implemented in python 2 under python 3K environment. To solve this problem, you have 2 solutions:
Find the python 3K compatible version of the library.
Make a virtualenv using python 2 as the default interpreter.
So i guess that your pip is from python3? In that case there is no easy fix as the library is clearly not compatibile with python3, even the print in error is not going to work, and that is just top of the iceberg.
You will either have to:
Downgrade your app to python2
Find a different library that can do the same
Port the library to python3.
Porting library to python3 may not be as scary as you think, especially since it seems that someone started to work on that already but I have no way of verifying how mature is that, but it for sure is a start.

Syntax error while running "yolk -l"

I have installed yolk 0.4.3 using pip. But when I tried yolk -l to display all installed packages, it showed a syntax error
File "C:\Python32\Lib\site-packages\yolk\cli.py" line 262
print "%s %s (%s)" % (project name,dis,version,
^
syntax error :invalid syntax
It seems you're using the package yolk (which only works Python 2). To install yolk for Python 3 use the yolk3k package:
pip install yolk3k
It looks like you are running a python 2 library with python 3 the versions need to match.
You have Python 2.x syntax with print when you are using Python 3.x (notice the "Python32" in "C:\Python32\Lib\site-packages\yolk\cli.py"). Below is an example written in Python 3.x:
>>> print "%s" % "a"
File "<stdin>", line 1
print "%s" % "a"
^
SyntaxError: invalid syntax
>>> print("%s" % "a")
a
>>>
As you can see, you need to use Python 3.x syntax (namely, treat print as a built-in, not a keyword).
To fix your problem, make sure your version of yolk works with your version of Python.

install error or error running python file

I am running a basic Python file on Windows XP from IDLE.
The file name is assignment1.py.
The file content is:
import sys
var = 5
but when I run it, it gives the error:
Command: python assignment1.py
SyntaxError: invalid syntax
Then I tried another thing which also gave an error:
Command: which python
SyntaxError: invalid syntax
Not sure if the installation is wrong or something.
I am able to run the print command successfully:
>>> print "I am working fine"
I am working fine
Not sure of the issue. Request help.
It looks like what you are entering as the "command" is being interpreted as Python code. I mean, python assignment1.py is interpreted as Python code. The result is as expected:
$ python -c 'python assignment1.py'
File "<string>", line 1
python assignment1.py
^
SyntaxError: invalid syntax
You need to run the file in the correct way, probably via the IDLE menu or by pressing F5. You can check these questions for details:
How do I run a Python program?
Running Python script from IDLE on Windows 7 64 bit

Categories

Resources