pymysql stopped working : NameError: name 'byte2int' is not defined - python-3.x

I am using pymysql in Python to connect to database. It was working fine but now I am getting following error :
Traceback (most recent call last) :
File "/Users/njethani/Desktop/venv/lib/python3.6/site-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/Users/njethani/Desktop/venv/lib/python3.6/site-packages/pymysql/connections.py", line 327, in __init__
self.connect()
File "/Users/njethani/Desktop/venv/lib/python3.6/site-packages/pymysql/connections.py", line 598, in connect
self._request_authentication()
File "/Users/njethani/Desktop/venv/lib/python3.6/site-packages/pymysql/connections.py", line 865, in _request_authentication
data = _auth.scramble_old_password(self.password, self.salt) + b'\0'
File "/Users/njethani/Desktop/venv/lib/python3.6/site-packages/pymysql/_auth.py", line 72, in scramble_old_password
hash_pass = _hash_password_323(password)
File "/Users/njethani/Desktop/venv/lib/python3.6/site-packages/pymysql/_auth.py", line 97, in _hash_password_323
for c in [byte2int(x) for x in password if x not in (' ', '\t', 32, 9)]:
File "/Users/njethani/Desktop/venv/lib/python3.6/site-packages/pymysql/_auth.py", line 97, in <listcomp>
for c in [byte2int(x) for x in password if x not in (' ', '\t', 32, 9)]:
NameError: name 'byte2int' is not defined
I am using following lines to connect to my database (connection string) :
conn = pymysql.Connect(host='hostname', port=3306, user='username', passwd='password', db='mysql')

Since the pymysql maintainer refuses to release the fix, the solution is simply to install an older version of the package:
pip3 install --user 'pymysql<0.9'

I got the same error and looked like _auth.py was unable to find the reference of byte2int. I added the below line to modify _auth.py to make it work.
from util import byte2int,int2byte
Please be aware the util.py is that of pymysql as there will be util.py files in other packages as well.

Related

Raise error, v # invalid expression when trying to create azure nic interface via az cli

I am trying to create Network interface on Azure using AZ Cli command and it is showing following error.
Ansible modules for the same have some hard requirements of urllib3 and requests package hence trying to use Azure cli.
The stacktrace looks like its a module issue.
/root/bin/lib/python2.7/site-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a release (2.7.7+) that supports hmac.compare_digest as soon as possible.
utils.PersistentlyDeprecated2018,
The command failed with an unexpected error. Here is the traceback:
nothing to repeat
Traceback (most recent call last):
File "/root/bin/lib/python2.7/site-packages/knack/cli.py", line 206, in invoke
cmd_result = self.invocation.execute(args)
File "/root/bin/lib/python2.7/site-packages/azure/cli/core/commands/__init__.py", line 523, in execute
self.commands_loader.load_arguments(command)
File "/root/bin/lib/python2.7/site-packages/azure/cli/core/__init__.py", line 291, in load_arguments
self.command_table[command].load_arguments() # this loads the arguments via reflection
File "/root/bin/lib/python2.7/site-packages/azure/cli/core/commands/__init__.py", line 290, in load_arguments
super(AzCliCommand, self).load_arguments()
File "/root/bin/lib/python2.7/site-packages/knack/commands.py", line 97, in load_arguments
cmd_args = self.arguments_loader()
File "/root/bin/lib/python2.7/site-packages/azure/cli/core/__init__.py", line 488, in default_arguments_loader
op = handler or self.get_op_handler(operation, operation_group=kwargs.get('operation_group'))
File "/root/bin/lib/python2.7/site-packages/azure/cli/core/__init__.py", line 528, in get_op_handler
op = import_module(mod_to_import)
File "/opt/rh/python27/root/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/root/bin/lib/python2.7/site-packages/azure/cli/command_modules/network/custom.py", line 23, in <module>
from azure.cli.command_modules.network.zone_file.parse_zone_file import parse_zone_file
File "/root/bin/lib/python2.7/site-packages/azure/cli/command_modules/network/zone_file/__init__.py", line 28, in <module>
from azure.cli.command_modules.network.zone_file.parse_zone_file import parse_zone_file
File "/root/bin/lib/python2.7/site-packages/azure/cli/command_modules/network/zone_file/parse_zone_file.py", line 77, in <module>
_COMPILED_REGEX = {k: re.compile(v, re.IGNORECASE) for k, v in _REGEX.items()}
File "/root/bin/lib/python2.7/site-packages/azure/cli/command_modules/network/zone_file/parse_zone_file.py", line 77, in <dictcomp>
_COMPILED_REGEX = {k: re.compile(v, re.IGNORECASE) for k, v in _REGEX.items()}
File "/root/bin/lib64/python2.7/re.py", line 190, in compile
return _compile(pattern, flags)
File "/root/bin/lib64/python2.7/re.py", line 242, in _compile
raise error, v # invalid expression
error: nothing to repeat```
Following packages I am using for az cli :
azure-cli 2.0.71
azure-cli-command-modules-nspkg 2.0.3
azure-cli-core 2.0.35
azure-cli-nspkg 3.0.2
azure-cli-telemetry 1.0.3
It seems your python version need to be upgraded, could you please upgrade your python to 2.7.7

Test cases are failing using mongomock pymongo mongoengine

I am trying to run test cases with mongomock having pymongo version as 2.X and mongoengine 0.10.6
I have tried to change the version for pymongo to 2.9.5 from 2.6.3 and mongoengine from 0.10.6 to 0.17.0. I have tried with mongomock from 3.0.0 to 3.17.0
#classmethod
def setUpClass(cls):
super(MongoTestCase, cls).setUpClass()
cls.conn = mongoengine.connect(
settings.MONGODB_NAME,
host=settings.MONGODB_DATABASE_HOST,
alias="default",
is_mock=True
)
I expect the test case to pass with message currently I am getting
======================================================================
ERROR: test_ibest_matches (packages.tests.test_utils.test_ibest_matches.TestIBestMatches)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/www/packages/tests/test_utils/test_ibest_matches.py", line 56, in test_ibest_matches
**context))
File "/www/packages/utils.py", line 228, in ibest_matches
for vc in valid_contexts
File "/www/packages/managers/base_package_manager.py", line 111, in best_matches
for group, packages in groupby(pkgs, key=lambda k: k.group):
File "/usr/lib/python2.7/site-packages/mongoengine/queryset/queryset.py", line 97, in _iter_results
self._populate_cache()
File "/usr/lib/python2.7/site-packages/mongoengine/queryset/queryset.py", line 116, in _populate_cache
self._result_cache.append(self.next())
File "/usr/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 1407, in next
raw_doc = self._cursor.next()
File "/usr/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 1482, in _cursor
**self._cursor_args)
TypeError: find() got an unexpected keyword argument 'fields'
----------------------------------------------------------------------
Ran 44 tests in 3.273s
FAILED (errors=26)

psutil.AccessDenied Error while trying to load StanfordCoreNLP

I'm trying to load the package StanfordCoreNLP to get the correct parsing for the movie reviews presented in their page (https://nlp.stanford.edu/sentiment/treebank.html): (I'm using MAC)
nlp = StanfordCoreNLP("/Users//NLP_models/stanford-corenlp-full-2018-01-31")
But get the error:
Traceback (most recent call last):
File "/Users/anaconda3/lib/python3.6/site-packages/psutil/_psosx.py", line 295, in wrapper
return fun(self, *args, **kwargs)
File "/Users/anaconda3/lib/python3.6/site-packages/psutil/_psosx.py", line 480, in connections
rawlist = cext.proc_connections(self.pid, families, types)
PermissionError: [Errno 1] Operation not permitted
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 2411, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1802, in run
launch(file, globals, locals) # execute the script
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
...
...
...
File "/Users/anaconda3/lib/python3.6/site-packages/stanfordcorenlp/corenlp.py", line 79, in __init__
if port_candidate not in [conn.laddr[1] for conn in psutil.net_connections()]:
File "/Users/anaconda3/lib/python3.6/site-packages/psutil/__init__.py", line 2191, in net_connections
return _psplatform.net_connections(kind)
File "/Users/anaconda3/lib/python3.6/site-packages/psutil/_psosx.py", line 229, in net_connections
cons = Process(pid).connections(kind)
File "/Users/anaconda3/lib/python3.6/site-packages/psutil/_psosx.py", line 300, in wrapper
raise AccessDenied(self.pid, self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=5488)
I tried
conda update conda
conda update anaconda-navigator
conda update navigator-updater
But it didn't help.
Any ideas??
Thanks!!
I have the same problem, and I got it work by running the code using sudo like below:
sudo /Users/edamame/workspace/git/chinese_nlp/venv/bin/python3 chinese_segmenter1.py
Hope this works for you as well.
Same problem here.
A lot of discussion of this points to https://github.com/ContinuumIO/anaconda-issues/issues/1984, which suggests updating to the latest Navigator, and running as root (via sudo).
I've tried both and see no change at all (you may be more fortunate).
https://github.com/Lynten/stanford-corenlp/issues/26 references a tweaked version of corenlp.py that claims to avoid the problem, though I haven't gotten it to work either.
This problem seems to be specific to Mac OS X which would not allow Python to check the current port.
Comment this portion of code of corenlp.py file:
if self.port is None:
for port_candidate in range(9000, 65535):
if port_candidate not in [conn.laddr[1] for conn in psutil.net_connections()]:
self.port = port_candidate
break
if self.port in [conn.laddr[1] for conn in psutil.net_connections()]:
raise IOError('Port ' + str(self.port) + ' is already in use.')
Replace by this line:
self.port = 9999
Source: https://github.com/Lynten/stanford-corenlp/issues/26#issuecomment-445507811
Another solution is to run StanfordCoreNLP with a sudo command line.

ArangoDB Connection error from python

Trying to connect to ArangoDB using python
Code:
from pyArango.connection import *
conn = Connection(username="root", password="XXXXX")
Got below error
Trace:
Traceback (most recent call last):
File "E:/Python/Nlp/imageTotext.py", line 177, in <module>
conn = Connection(username="root", password="XXXXX") File "C:\Users\am843946\AppData\Roaming\Python\Python27\site-packages\pyArango\connection.py",
line 105, in __init__
self.reload() File "C:\Users\am843946\AppData\Roaming\Python\Python27\site-packages\pyArango\connection.py",
line 124, in reload
data = r.json() File "C:\Users\am843946\AppData\Roaming\Python\Python27\site-packages\pyArango\connection.py",
line 19, in __call__
raise e ValueError: No JSON object could be decoded
did you try to specify a connection URL like that?
conn = Connection(arangoURL="http://localhost:8529", username="root", password="")
That way it should work.

TypeError: Can't convert 'bytes' object to str implicitly for tweepy

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
ckey=''
csecret=''
atoken=''
asecret=''
class listener(StreamListener):
def on_data(self,data):
print(data)
return True
def on_error(self,status):
print(status)
auth = OAuthHandler(ckey,csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track="cricket")
This code filter the twitter stream based on the filter. But I am getting following traceback after running the code. Can somebody please help
Traceback (most recent call last):
File "lab.py", line 23, in <module>
twitterStream.filter(track="car".strip())
File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 430, in filter
self._start(async)
File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 346, in _start
self._run()
File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 286, in _run
raise exception
File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 255, in _run
self._read_loop(resp)
File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 298, in _read_loop
line = buf.read_line().strip()
File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 171, in read_line
self._buffer += self._stream.read(self._chunk_size)
TypeError: Can't convert 'bytes' object to str implicitly
Im assuming you're using tweepy 3.4.0. The issue you've raised is 'open' on github (https://github.com/tweepy/tweepy/issues/615).
Two work-arounds :
1)
In streaming.py:
I changed line 161 to
self._buffer += self._stream.read(read_len).decode('UTF-8', 'ignore')
and line 171 to
self._buffer += self._stream.read(self._chunk_size).decode('UTF-8', 'ignore')
and then reinstalled via python3 setup.py install on my local copy of tweepy.
2)
remove the tweepy 3.4.0 module, and install 3.3.0 using command: pip install -I tweepy==3.3.0
Hope that helps,
-A
You can't do twitterStream.filter(track="car".strip()). Why are you adding the strip() it's serving no purpose in there.
track must be a str type before you invoke a connection to Twitter's Streaming API and tweepy is preventing that connection because you're trying to add strip()
If for some reason you need it, you can do track_word='car'.strip() then track=track_word, that's even unnecessary because:
>>> print('car'.strip())
car
Also, the error you're getting does not match the code you have listed, the code that's in your question should work fine.

Resources