ignore Python SyntaxError coming from `PYTHONWARNINGS="error::Warning`? - python

I'm using PYTHONWARNINGS="error::Warning" to raise exceptions on warnings, but I want to ignore certain such exceptions, which is normally fine. When the exception that results is SyntaxError, I would sometimes like to ignore that but don't know how.
Detailed example:
# has_deprecated_syntax.py
"""this becomes invalid: \* """
Then:
`$PYTHONWARNINGS="error::Warning" python3.6 -c "import has_deprecated_syntax"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/davidchudzicki/temp/so/has_deprecated_syntax.py", line 1
"""this becomes invalid: \* """
^
SyntaxError: invalid escape sequence \*
These attempts to ignore it both got me the same failure:
PYTHONWARNINGS="error::Warning,default::Warning:has_deprecated_syntax" python3.6 -c "import has_deprecated_syntax"
and
PYTHONWARNINGS="error::Warning,default::SyntaxError" python3.6 -c "import has_deprecated_syntax"
For reference, here's what makes me think this is the correct environment setting in general for ignoring warnings from particular modules:
With:
# has_warning.py
import warnings
warnings.warn("hi")
... I get:
$PYTHONWARNINGS="error::Warning" python -c "import has_warning"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "has_warning.py", line 3, in <module>
warnings.warn("hi")
UserWarning: hi
...which I can ignore with:
$PYTHONWARNINGS="error::Warning,default::Warning:has_warning" python -c
"import has_warning"
has_warning.py:3: UserWarning: hi
warnings.warn("hi")
But it doesn't work for SyntaxError. Is there any way to ignore those from particular modules?
(Note: I've constructed a small example for this question, but the real reason I care is that I want to fail on warnings in continuous integration for package I help with, but I get the SyntaxError from one of our dependencies when I do this.)

You can't ignore SyntaxError. It's not a warning, it's an error.

Related

How to change python command to use python2 again

I'm running a script and I get a comma error:
dbconfigure -a abc -d ...
Traceback (most recent call last):
File "/usr/local/bin/dbconf", line 6, in <module>
from dbconf import main
File "/usr/local/lib/python3.7/site-packages/dbconf/__init__.py", line 308
except SDBResponseError, exc:
^
SyntaxError: invalid syntax
So I think the script uses a python command but the python command on my computer points to a python3 version. How do I fix this?
You can put the shebang header on Unix (including mac os). First line of your script should be #! /usr/bin/env python2

Jedi-vim doesn't work

I have a issue with VIM. I'm trying to install jedi-vim according to https://github.com/davidhalter/jedi-vim
but when I open vim, it report such error:
initialize.py" 25L, 831C'import site' failed; use -v for traceback
Error detected while processing /home/.../.vim/autoload/jedi.vim:
line 285
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/.../.vim/initialize.py", line 10, in <module>
import os
ImportError: No module named os
And when I use Ctrl+Space in vim, it results in
Error detected while processing function jedi#completions:
line 1:
Traceback (most recent call last):
Press ENTER or type command to continue
In fact, I don't get any error when importing os from the command line..
import os
os,
module 'os' from '/usr/local/install/python-2.7.5/lib/python2.7/os.pyc'
Anybody know how to solve this error?Thank You.
Pretty sure that there's something wrong with your VIM. You're probably using vim on mac and there's something wrong with the sys.path.
You should be able to use :python import os in VIM. If that doesn't work, Jedi certainly won't.

Warning: 'as' will become a reserved keyword in Python 2.6

while running npm install (e.g. https://github.com/donpark/html2jade), I run into this error:
/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/input.py:852: Warning: 'as' will become a reserved keyword in Python 2.6
Traceback (most recent call last):
File "/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp", line 15, in <module>
import gyp
File "/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/__init__.py", line 8, in <module>
import gyp.input
File "/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/input.py", line 852
except ImportError as e:
^
SyntaxError: invalid syntax
python --version generates 'Python 2.7.1'
This would not be a syntax error in Python 2.7.1. A quick workaround would be to run this programme as python /full/path/to/scriptname.py, which will actually use Python 2.7.1.

Installing unicode csv for Python?

I'm new to github and I'm trying to install unicodecsv (https://github.com/jdunck/python-unicodecsv).
I'm trying
sudo pip install -e git://github.com/jdunck/python-unicodecsv.git#egg=unicodecsv
But I'm getting an error message. I'm probably doing someone basic wrong, can someone help?
Obtaining unicodecsv from git+git://github.com/jdunck/python-unicodecsv.git#egg=unicodecsv
Cloning git://github.com/jdunck/python-unicodecsv.git to ./src/unicodecsv
Running setup.py egg_info for package unicodecsv
Traceback (most recent call last):
File "<string>", line 14, in <module>
File "/Users/dave/Dropbox/Promoter/working/src/unicodecsv/setup.py", line 5, in <module>
version = __import__('unicodecsv').__version__
File "unicodecsv/__init__.py", line 49
except TypeError, e:
^
SyntaxError: invalid syntax
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 14, in <module>
File "/Users/dave/Dropbox/Promoter/working/src/unicodecsv/setup.py", line 5, in <module>
version = __import__('unicodecsv').__version__
File "unicodecsv/__init__.py", line 49
except TypeError, e:
^
SyntaxError: invalid syntax
----------------------------------------
Command python setup.py egg_info failed with error code 1
Storing complete log in /Users/dave/.pip/pip.log
Davids-MacBook-Air:working dave$ sudo pip install -e git://github.com/jdunck/python-unicodecsv.git#egg=unicodecsv
Obtaining unicodecsv from git+git://github.com/jdunck/python-unicodecsv.git#egg=unicodecsv
Updating ./src/unicodecsv clone
^[ Running setup.py egg_info for package unicodecsv
Traceback (most recent call last):
File "<string>", line 14, in <module>
File "/Users/dave/Dropbox/Promoter/working/src/unicodecsv/setup.py", line 5, in <module>
version = __import__('unicodecsv').__version__
File "unicodecsv/__init__.py", line 49
except TypeError, e:
^
SyntaxError: invalid syntax
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 14, in <module>
File "/Users/dave/Dropbox/Promoter/working/src/unicodecsv/setup.py", line 5, in <module>
version = __import__('unicodecsv').__version__
File "unicodecsv/__init__.py", line 49
except TypeError, e:
^
SyntaxError: invalid syntax
----------------------------------------
Command python setup.py egg_info failed with error code 1
Storing complete log in /Users/dave/.pip/pip.log
The problem is that you're using Python 3.0 or later, and trying to use a library which is only compatible with 2.7 and earlier.
The specific problem is the line the traceback points at:
except TypeError, e:
This syntax was deprecated in 2.6, in favor of (more flexible and more consistent) new syntax:
except TypeError as e:
In 3.0 and later, the deprecated syntax is no longer allowed at all.
So, if you want to use this library, someone will have to port it—you, the author, or someone else. It may just be a matter of running 2to3, or fixing each except statement manually—but it may be a lot more to do than that, especially considering this library is all about Unicode.
However, it's worth noting that Python 3.x doesn't have the same problem as 2.x did. You can pass the csv module text/Unicode file objects, and it will just handle them. Adapting the example from the unicodecsv docs:
>>> import csv
>>> from io import StringIO
>>> f = StringIO()
>>> w = csv.writer(f)
>>> w.writerow(('é', 'ñ'))
>>> f.seek(0)
>>> r = csv.reader(f)
>>> row = r.next()
>>> print row[0], row[1]
é ñ
Note that I didn't even have to specify utf-8, because StringIO is a Unicode str buffer, not a bytes buffer. You don't have to worry about coding at all.
If you didn't even know you were running Python 3.x (as in, you knew you installed it, but you were careful to keep Apple's pre-installed Python 2.7 higher on the PATH), there are three common reasons this can happen.
Apple's Python doesn't come with pip; all of the popular Python 3.x installers and packages do. So, if you haven't installed pip for 2.7, the only one you have is 3.x.
Apple's Python installs scripts like pip to /usr/local/bin. So do some of the popular Python 3.x installers and packages. So, whichever you installed more recently wins. (The 3.x one should also be available as /usr/local/bin/pip3, so overwriting its /usr/local/bin/pip with 2.7's is usually fine… unless pip3 is a symlink to pip.)
sudo deliberately discards most of your user environment, so 2.7 may be higher on your PATH when running as yourself, but not when running with sudo.

Error installing selenium python

Getting this error when running pip install -U selenium. Mid way through the script, it gets the following SyntaxError:
Traceback (most recent call last):
File "<string>", line 14, in <module>
File "C:\Python32\Scripts\build\rdflib\setup.py", line 6, in <module>
from rdflib import __version__
File "rdflib\__init__.py", line 64, in <module>
from rdflib.term import URIRef, BNode, Literal, Variable
File "rdflib\term.py", line 367
except TypeError, te:
^
SyntaxError: invalid syntax
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 14, in <module>
File "C:\Python32\Scripts\build\rdflib\setup.py", line 6, in <module>
from rdflib import __version__
File "rdflib\__init__.py", line 64, in <module>
from rdflib.term import URIRef, BNode, Literal, Variable
File "rdflib\term.py", line 367
except TypeError, te:
^
SyntaxError: invalid syntax
----------------------------------------
Command python setup.py egg_info failed with error code 1
Since it is a Syntax Error, I assume it is a python version problem, I'm running 3.2.2.
Pip did come with a pip-3.2.exe file, which I tried to run. But I got the same error. I'm pretty new to Python so it might be something simple.
And how can it be a syntaxError? pip is an already made program.
Running Win7, Python 3.2.2
Selenium does not support Python 3. Use Python 2.7 instead.
Sadly, almost all Python software requires Python 2.x.
[Update]
Selenium supports Python 3.x now.

Categories

Resources