invalid level choice: choose from parser.log_levels - python

I am trying to make use of an open source Python program. I have a Python script configured exactly as per an example with all the pre-reqs installed, but I seem to be getting some kind of global Python error:
Traceback (most recent call last):
File "/etc/asterisk/scripts/detect/gong/gonglearn.py", line 15, in <module>
from pyAudioAnalysis.audioTrainTest import extract_features_and_train
File "/root/pyAudioAnalysis/pyAudioAnalysis/audioTrainTest.py", line 9, in <module>
from pyAudioAnalysis import MidTermFeatures as aF
File "/root/pyAudioAnalysis/pyAudioAnalysis/MidTermFeatures.py", line 8, in <module>
from pyAudioAnalysis import audioBasicIO
File "/root/pyAudioAnalysis/pyAudioAnalysis/audioBasicIO.py", line 6, in <module>
import eyed3
File "/usr/local/lib/python2.7/dist-packages/eyed3/__init__.py", line 31, in <module>
from .utils.log import log # noqa: E402
File "/usr/local/lib/python2.7/dist-packages/eyed3/utils/__init__.py", line 361
msg = f"invalid level choice: {level} (choose from {parser.log_levels})"
SyntaxError: invalid syntax
I went to look at the code for __init__.py and I found:
def __call__(self, parser, namespace, values, option_string=None):
values = values.split(':')
level, logger = values if len(values) > 1 else (values[0],
self.main_logger)
logger = logging.getLogger(logger)
try:
logger.setLevel(logging._nameToLevel[level.upper()])
except KeyError:
msg = f"invalid level choice: {level} (choose from {parser.log_levels})"
raise argparse.ArgumentError(self, msg)
super(LoggingAction, self).__call__(parser, namespace, values, option_string)
Since this appears to be system code, or something with eyed3, not specific to what I am trying to run, I am not really sure how to proceed. I can't find any references anywhere on how to debug this particular error.
What problem exactly is this indicating? I'm usually able to resolve syntax errors myself, but in this case the message seems a bit cryptic and I can't decipher it. I tried importing various other things into my script and doing import logging as log but neither of those has helped any. Do I have to somehow configure logging in Python before I can use it?
I apologize if this is a weird issue, but it seems fairly obscure, and it's been a while since I've used Python before, and that was on Windows.
I am running Python (not Python 3) on Debian 10.

The problem here is with the line suggested in the traceback:
msg = f"invalid level choice: {level} (choose from {parser.log_levels})"
The issue here is that this line is using f-strings, to do string interpolation. The reason this is a problem is that f-strings weren't introduced until python 3.6.
Unfortunately, because this is code from eyed3 you can't change it. You do have a few options though:
Upgrade your version of python to at least 3.6 (probably the best option)
fork the library, and edit that line so that you can use it with your version of python. Install your forked version. (This is probably a bad idea, this is unlikely to be the only place where there are incompatibilities. You will likely have to update quite a lot. It'll be easier to just update the version of python you're using).
If you look at the getting started section of the eyeD3 repo, it says:
Python >= 3.6 is required.

Related

jpype._jclass.NoClassDefFoundError: edu/stanford/nlp/python/SUTimeWrapper

I'm trying to use the sutime python wrapper to make a date normalizer, that would convert any temporal information in strings into dates in the format YYYY-MM-DD. I've created a class, with rules over the sutime outputs to convert the sutime outputs into the standard format as mentioned above. The program is working properly on my local machine, but when i try to run it on a server I get the jpype._jclass.NoClassDefFoundError. The server is on ubuntu with python2, while my local has windows, with python3.
I've tried to implement the solutions to a similar problem on this https://sourceforge.net/p/jpype/discussion/379372/thread/689d7a9b/ forum, but i'm not sure if i was able to implement these soultions correctly. I've also checked that sutime supports both python3 and python2
I think the issue is with jpype or with the sutime library.
This is the traceback that i got
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "date_normalizer.py", line 38, in __init__
self.sutime = SUTime(jars=self.jar_files, mark_time_ranges=mark_time_ranges)
File "/home/bridgei2i/.local/lib/python2.7/site-packages/sutime/sutime.py", line 57, in __init__
'edu.stanford.nlp.python.SUTimeWrapper')
File "/home/bridgei2i/.local/lib/python2.7/site-packages/jpype/_jclass.py", line 130, in __new__
return _JClassNew(args[0], **kwargs)
File "/home/bridgei2i/.local/lib/python2.7/site-packages/jpype/_jclass.py", line 213, in _JClassNew
javaClass = _jpype.PyJPClass(arg)
jpype._jclass.NoClassDefFoundError: edu/stanford/nlp/python/SUTimeWrapper
Seems likely that the jar file holding edu/stanford/nlp/python/SUTimeWrapper was not found on the server. The specific code that failed was a call to JClass('edu.stanford.nlp.python.SUTimeWrapper') which is a request to load a class from a jar. I would recommend checking the classpath and configuration on the server.
Likely causes are (in order of likelihood)
jar file is not located in the classpath on the server.
The jar file is compiled with a JDK which is newer than runtime environment (though this should generate a different exception).
Some jar file that the class depends on is missing or has the wrong version. (this should produce a different classname in the exception, so it is unlikely.)
A dll for a native portion the jar file is missing or has an incorrect architecture. (rare)
Assuming the jar file is on the server, I would recommend checking the initialization in which the JPype startJVM call is made to see if the path to the jar was correct. It is also possible to examine the loaded classpath using print(jpype.java.lang.System.getProperty('java.class.path')) to see if there is a difference between your local and server machine.
thank you, as you said:Some jar file that the class depends on is missing or has the wrong version. (this should produce a different classname in the exception, so it is unlikely.)

Problem with using Python library - Pyotodom

I wanted to use this library to scrape data from otodom. I've read the docs however I'm stuck on a very basic level, trying to do basic import. The code I use is taken directly from the docs. On top of that I've tried to launch example.py from Github but I'm getting the same error.
Here's the excerpt from the code:
import otodom, os, logging
from otodom.category import get_category
from otodom.offer import get_offer_information
Error message I get:
Traceback (most recent call last):
File ".\otodom_import.py", line 2, in
from otodom.category import get_category
File "C:\Users\Dom\Anaconda3\lib\site-packages\otodom\category.py", line 9, in
from otodom.utils import get_response_for_url, get_url
File "C:\Users\Dom\Anaconda3\lib\site-packages\otodom\utils.py", line 14, in
from scrapper_helpers.utils import caching, normalize_text, key_sha1,
get_random_user_agent
File "C:\Users\Dom\Anaconda3\lib\site-packages\scrapper_helpers\utils.py",
line 22, in
MAX_FILENAME_LENGTH = subprocess.check_output("getconf NAME_MAX /",
shell=True).strip()
File "C:\Users\Dom\Anaconda3\lib\subprocess.py", line 336, in check_output
**kwargs).stdout
File "C:\Users\Dom\Anaconda3\lib\subprocess.py", line 418, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'getconf NAME_MAX /' returned non-zero
exit status 1.
Thanks in advance!
EDIT: Don't understand downvotes, I realize that this is a niche module but I did my homework. I've read the docs and tried to find help in other places. If there is a chance that as a noobie I'll find at least one person that can help me, why wouldn't I use this opportunity? Even info that module is buggy can help.
getconf, which the dependency scrapper_helpers module is trying to call, is a POSIX command not available on Windows.
You can track it down in C:\Users\Dom\Anaconda3\lib\site-packages\scrapper_helpers\utils.py and replace the line 22 with:
MAX_FILENAME_LENGTH = 255
But then the question is what other error might pop up given that the module obviously expects to run on a POSIX-compatible system - for example, it will attempt to use /var/tmp/scrapper-helpers/ as its temporary cache path which is also not available on Windows (but Python will interpret it as <DRIVE_LETTER>:\var\tmp\scrapper-helpers and attempt to create it).
Alternatively, looking at its code, you might get away with just declaring the following environment variables:
set MAX_FILENAME_LENGTH=255
set CACHE_DIR=%TEMP%\scapper-helpers\

pylint disabling a single line of code just produces another pylint error

The documentation in section 4.1 clearly states:
https://pylint.readthedocs.io/en/latest/faq.html#message-control
4.1 Is it possible to locally disable a particular message?
Yes, this feature has been added in Pylint 0.11. This may be done by adding “#pylint: disable=some-message,another-one” at the desired block level or at the end of the desired line of code
Great! but it doesn't work. Boo.
I get the the following pylint error for the following line of code
W: 26, 2: Redefining built-in 'zip' (redefined-builtin)
try:
from itertools import izip as zip # pylint: disable=bad-builtin
except ImportError:
pass
But pylint just complains even louder about my attempt to shut it up:
E: 26, 0: Bad option value 'bad-builtin' (bad-option-value)
I've also tried the error code # pylint: disable=W0141, that also produces a similar error.
Any idea what I'm doing wrong?
I have been in a similar situation.
Unsolvable pylint issue
class A:
pass
There are many warnings in pylint for the code above, but I want to talk about old-style-class.
In Python 2.7, you will get an old-style-class error.
Of course, you can change your code like this:
class A(object):
pass
However, you will receive a useless-object-inheritance warning in Python 3.
If you are writing a package compatible with python 2.7 and 3 and using pylint, then you are down.
Unavoidable bad-option-value
Yes, if it is accepted to disable either of old-style-class or useless-object-inheritance in a comment, you can go further.
In Python 2.7:
# pylint: disable=old-style-class
class A:
pass
In Python 3:
# pylint: disable=useless-object-inheritance
class A(object):
pass
Eventually, you will get a bad-option-value, just the same as this question.
Disable bad-option-value
I have tried, but bad-option-value can not be disabled locally in this case.
I have to disable bad-option-value in a pylint configuration file, like .pylintrc.
[TYPECHECK]
disable=bad-option-value
Note: My pylint version is 1.9.4 in python 2.7, 2.2.2 in python 3.
Ah, simple answer, it should be # pylint: disable=bad-option-value which is presented in the error message in parenthesis:
E: 26, 0: Bad option value 'bad-builtin' (bad-option-value)
When you get this message:
W: 26, 2: Redefining built-in 'zip' (redefined-builtin)
You have to disable the exact error message you are getting (the one in parenthesis):
try:
from itertools import izip as zip # pylint: disable=redefined-builtin
except ImportError:
pass
That seems to work fine in pylint 2.5.
It can be annoying if you are testing with multiple versions of python or different venvs and the same code base and you get different errors. Be sure you fix the version to one version across all your builds/tests. It sounds like that may have happened here (not sure where you got bad-builtin from).

GDB printing STL data

After following the instructions given on this site: https://sourceware.org/gdb/wiki/STLSupport
GDB is still unable to print the contents of stl containers like vectors, other than printing out a huge amount of useless information. When GDB loads, I also get the following errors, which I think are related to the Python that I put into ~/.gdbinit
Traceback (most recent call last):
File "<string>", line 4, in <module>
File "/Users/mayankp/gdb_printers/python/libstdcxx/v6/printers.py", line 1247, in register_libstdcxx_printers
gdb.printing.register_pretty_printer(obj, libstdcxx_printer)
File "/usr/local/share/gdb/python/gdb/printing.py", line 146, in register_pretty_printer
printer.name)
RuntimeError: pretty-printer already registered: libstdc++-v6
/Users/mayankp/.gdbinit:6: Error in sourced command file:
Error while executing Python code.
When GDB loads, I also get the following errors...
It looks like instructions you followed on https://sourceware.org/gdb/wiki/STLSupport are invalid now. If you look at svn log you will see that registering of pretty printers was added in __init__.py recently:
------------------------------------------------------------------------
r215726 | redi | 2014-09-30 18:33:27 +0300 (Вт., 30 сент. 2014) | 4 lines
2014-09-30 Siva Chandra Reddy <sivachandra#google.com>
* python/hook.in: Only import libstdcxx.v6.
* python/libstdcxx/v6/__init__.py: Load printers and xmethods.
------------------------------------------------------------------------
And therefore second registration throws error. You can remove it or comment out:
#register_libstdcxx_printers (None)
GDB is still unable to print the contents of stl containers
You have probably mismatched pretty printers with your gcc. See https://stackoverflow.com/a/9108404/72178 for details.
From your traceback it seems that the register_libstdcxx_printers() call is failing because there already is such a pretty printer registered. To avoid that, you can wrap it in a try..except to make sure instructions in .gdbinit don't interfere with the launch of GDB if they fail:
python
import sys
sys.path.insert(0, '/home/user/gdb_printers/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
try:
register_libstdcxx_printers(None)
except:
pass
end
(Note: You should usually never use a bare except statement without qualifying the type of exceptions you want to catch. This is a special case though, in startup configuration files like .gdbinit, .pdbrc or your PYTHONSTARTUP file you'll probably want to write defensive code like that).
But chances are this will only get rid of the ugly traceback for you, and printing of STL vectors still wont work. Because it seems there is already a pretty printer registered from somewhere else.
Make sure the path /home/user/gdb_printers/python actually matches the path where you checked out the module mentioned in the STLSupport docs.

How to port a Python application to Linux that works fine in Windows

I am having trouble porting a working, Windows Python application to Linux. I am having some problems, because I did not write the code and am just learning Python. I am having trouble fixing the issues that it keeps throwing up. So here is a kind of error that right now I am stuck with
Traceback (most recent call last):
File "alpha_beta", line 237, in <module>
main()
File "alpha_beta", line 185, in main
ABCCmd()
File "alpha_beta.py", line 74, in ABCCmd
File "C:\softs\Python\Lib\shutil.py", line 80, in copy
File "C:\softs\Python\Lib\shutil.py", line 47, in copyfile
IOError: [Errno 13] Permission denied: '/myPath/XFiles.bin.addr_patched
Any pointers on how to fix it will be much appreciated
Edit:
1) What I mean by I am stuck is, the traceback of the error goes to C:\softs\Python\Lib but actually I am currently executing this code in Ubuntu. Why would the traceback reference to windows library
2) Another thing that bothers me is it says there is an IOError.But when I try to add permission for the denied one it gives me a chmod: changing permissions of /myPath/xFiles.bin.addr_patched': Operation not permitted
Edit 2:
I had commented out a module because I thought it wasn't very useful. Since Now I am anyway discussing the porting issues, I thought I can bring up this additional problem as well since I think the issue is the same and the fix should be similar. On including #pdb module in the python code, I get the following error
traceback (most recent call last):
File "alpha_beta", line 6, in <module>
import pdb
File "C:\softs\Python\Lib\pdb.py", line 14, in <module>
File "C:\softs\Python\Lib\pprint.py", line 39, in <module>
ImportError: No module named cStringIO
I looked at the importer_Cache and looks like this
'': None, '/usr/lib/python2.6/plat-linux2': None, '/usr/lib/python2.6/': None
'/usr/lib/pymodules/python2.6/gtk-2.0': None, '/usr/lib/python2.6/lib-tk': None,
'/usr/lib/python2.6/lib-old': <imp.NullImporter object at 0x7f1269048070>, '/usr/
/python2.6/dist-packages/gtk-2.0': None, '/usr/lib/python2.6/dist-packages/PIL': None,
'/usr/local/lib/python2.6/dist-packages': None, '/usr/lib/python2.6/dist-packages':
None
*SOLVED : There was a weird wrapper in the script which was causing the python script to execute from a different location. Still don't understand how though. After modifying my script to just directly execute as python myScript.py it runs fine. Thank you all anyway for the help
The mixture of Windows and Unix style paths in your error messages makes me think that you may have some filenames that are hard coded or manually created using os specific path separators ('\' or '/'). If you can figure out where the gOptions.inputTf and gWorkingTfFile values are assigned you should look into using os.path.join to help you use the correct paths.
from os.path import join
file = join('A', 'B')
# 'A/B' on unix systems
# 'A\B' on windows systems

Categories