Python 3.X pycomm.ab_comm.clx CommError: must be str, not bytes - python-3.x

Im trying to get "PYCOMM" to connect to my CLX 5000 processor.
Every time I run my code I get:CommError: must be str, not bytes.
I have looked all over the code and I cant find where the issue is. Everything that is supposed to be in a string format is.
I am using python3.6
Here is the code:
import sys
from pycomm.ab_comm.clx import Driver as ClxDriver
c = ClxDriver()
if c.open('172.16.2.161'):
print(c.read_tag('Start'))
# Prints (1,'BOOL') if true; (0,'BOOL') if false
c.close()
Here is the error:
C:\Users\shirley\Miniconda3\python.exe C:/Users/shirley/Downloads/pycomm-pycomm3/pycomm-pycomm3/examples/test_clx_comm.py
Traceback (most recent call last):
File "C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 617, in build_header
h += pack_uint(length) # Length UINT
TypeError: must be str, not bytes
The header is 24 bytes fixed length, and includes the command and the length of the optional data portion.
:return: the headre
"""
try:
h = command # Command UINT
**h += pack_uint(length) # Length UINT**
h += pack_dint(self._session) # Session Handle UDINT
h += pack_dint(0) # Status UDINT
h += self.attribs['context'] # Sender Context 8 bytes
h += pack_dint(self.attribs['option']) # Option UDINT
return h
except Exception as e:
raise CommError(e)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 786, in open
if self.register_session() is None:
File "C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 635, in register_session
self._message = self.build_header(ENCAPSULATION_COMMAND['register_session'], 4)
File "C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 624, in build_header
raise CommError(e)
pycomm.cip.cip_base.CommError: must be str, not bytes
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/shirley/Downloads/pycomm-pycomm3/pycomm-pycomm3/examples/test_clx_comm.py", line 5, in
if c.open('172.16.2.161'):
File "C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 793, in open
raise CommError(e)
pycomm.cip.cip_base.CommError: must be str, not bytes
Process finished with exit code 1

After banging my head against wall trying to figure this out on my own.
I just switched to using PYLOGIX
https://github.com/dmroeder/pylogix
It worked the first time I ran it
and its reasonably fast.

I had the same problem:
TypeError: must be str, not bytes.
The error is because you are using Python 3.x, and I did the same thing: I used Python 3.6 instead of Python 2.6 or 2.7.
Change Python 3.x to 2.6 or 2.7 (I used 2.7).

This was a bug that was fixed recently
I have it working with pycomm#1.0.8 and python#3.7.3

Related

How can I remove Attribute error from my Csv program showing? [duplicate]

So I copied and pasted a demo program from the book I am using to learn Python:
#!/usr/bin/env python
import csv
total = 0
priciest = ('',0,0,0)
r = csv.reader(open('purchases.csv'))
for row in r:
cost = float(row[1]) * float(row[2])
total += cost
if cost == priciest[3]:
priciest = row + [cost]
print("You spent", total)
print("Your priciest purchase was", priciest[1], priciest[0], "at a total cost of", priciest[3])
And I get the Error:
Traceback (most recent call last):
File "purchases.py", line 2, in <module>
import csv
File "/Users/Solomon/Desktop/Python/csv.py", line 5, in <module>
r = csv.read(open('purchases.csv'))
AttributeError: 'module' object has no attribute 'read'
Why is this happening? How do I fix it?
Update:
Fixed All The Errors
Now I'm getting:
Traceback (most recent call last):
File "purchases.py", line 6, in <module>
for row in r:
_csv.Error: line contains NULL byte
What was happening in terms of the CSV.py:
I had a file with the same code named csv.py, saved in the same directory. I thought that the fact that it was named csv .py was screwing it up, so I started a new file called purchases.py, but forgot to delete csv
Don't name your file csv.py.
When you do, Python will look in your file for the csv code instead of the standard library csv module.
Edit: to include the important note in the comment: if there's a csv.pyc file left over in that directory, you'll have to delete that. that is Python bytecode which would be used in place of re-running your csv.py file.
There is a discrepancy between the code in the traceback of your error:
r = csv.read(open('purchases.csv'))
And the code you posted:
r = csv.reader(open('purchases.csv'))
So which are you using?
At any rate, fix that indentation error in line 2:
#!/usr/bin/env python
import csv
total = 0
And create your csv reader object with a context handler, so as not to leave the file handle open:
with open('purchases.csv') as f:
r = csv.reader(f)

How to get the actual file name in exception message in Databricks?

I am trying to figure out, how to get the actual file/module name in exception message in Databricks.
import traceback
def bad():
print("hello")
a = 1/0
def main():
try:
bad()
except Exception as e:
print(traceback.format_exc())
main()
When I run this the exception message I get like -
hello
Traceback (most recent call last):
File "<command-162594828857243>", line 8, in main
bad()
File "<command-162594828857243>", line 4, in bad
a = 1/0
ZeroDivisionError: division by zero
The "<command-162594828857243>" doesn't help at the time of debugging. I want the actual file/module name there.

python pyttsx3 error -- _pickle.UnpicklingError: invalid load key, '\x00'

i am trying to convert text to speech using pyttsx3 in python. but iam getting the error -- _pickle.UnpicklingError: invalid load key, '\x00'.
it worked once. later it didn't
my code
import pyttsx3
engine = pyttsx3.init()
engine.say("I will speak this text")
engine.runAndWait()
error i am receiving is --
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\pyttsx3__init__.py",
line 20, in init
eng = _activeEngines[driverName]
File "C:\ProgramData\Anaconda3\lib\weakref.py", line 137, in
getitem
o = self.data[key]()
KeyError: None
During handling of the above exception, another exception occurred:
...
File "C:\ProgramData\Anaconda3\lib\site-packages\win32com\client\gencache.py", line 113, in _LoadDicts
version = p.load()
_pickle.UnpicklingError: invalid load key, '\x00'.
python version is 3.7.3 |
pyttsx3 version is 2.71|
pywin32 version is 224
please help
I had this problem as well and fixed it by deleting gen_py in my temp directory.
You can find this folder here:
C:\Users\USERNAME\AppData\Local\Temp\gen_py

Python cmd shell: handling undefined commands

I'm writing a shell that must be able to take an unlimited number of commands. I can't figure out from the docs (https://docs.python.org/3.6/library/cmd.html) which say:
Cmd.default(line) Method called on an input line when the command
prefix is not recognized. If this method is not overridden, it prints
an error message and returns.
I must be writing the default() method incorrectly?
I've tried this:
import cmd
class MyShell(cmd.Cmd):
def default():
print('you entered this unknown command: ')
if __name__ == '__main__':
MyShell().cmdloop()
but get this (when I enter 'hi' in the shell):
(Cmd) hi
Traceback (most recent call last):
File "/Users/david/anaconda/lib/python3.6/cmd.py", line 214, in onecmd
func = getattr(self, 'do_' + cmd)
AttributeError: 'MyShell' object has no attribute 'do_hi'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "bitmessage_shell.py", line 9, in <module>
MyShell().cmdloop()
File "/Users/david/anaconda/lib/python3.6/cmd.py", line 138, in cmdloop
stop = self.onecmd(line)
File "/Users/david/anaconda/lib/python3.6/cmd.py", line 216, in onecmd
return self.default(line)
TypeError: default() takes 0 positional arguments but 2 were given
def default():
print('you entered this unknown command: ')
that doesn't work in a class. You need at least the object argument (self), or make the method static/class using #staticmethod or #classmethod decorators (but not very convenient, you may need the object state at some point)
Moreover, the parent class seems to pass the line, so now you need 2 arguments:
def default(self,line):
print('you entered this unknown command: ',line)

Python http.client.Incomplete Read(0 bytes read) error

I have seen this error on the forum and read through the responses yet I still don't understand what it is or how to address it. I'm scraping data from the internet from 16k links, my script scrapes similar information from each link and writes it to a .csv some of the date gets written before this error.
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 541, in _get_chunk_left
chunk_left = self._read_next_chunk_size()
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 508, in _read_next_chunk_size
return int(line, 16)
ValueError: invalid literal for int() with base 16: b''
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 558, in _readall_chunked
chunk_left = self._get_chunk_left()
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 543, in _get_chunk_left
raise IncompleteRead(b'')
http.client.IncompleteRead: IncompleteRead(0 bytes read)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "MoviesToDb.py", line 91, in <module>
html = r.read()
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 455, in read
return self._readall_chunked()
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 565, in _readall_chunked
raise IncompleteRead(b''.join(value))
http.client.IncompleteRead: IncompleteRead(17891 bytes read)
I would like to know:1) What does this error mean? 2) How do I prevent it?
try to import :
from http.client import IncompleteRead
and add this in your script :
except IncompleteRead:
# Oh well, reconnect and keep trucking
continue
requests.exceptions.ChunkedEncodingError: (‘Connection broken: IncompleteRead(0 bytes read)’, IncompleteRead(0 bytes read)).
It is because the server of http protocal is 1.0 version,while python use 1.1 version. The solution is to assign the protocal version of client, like this
Python3 Version please add:
> import http.client
> http.client.HTTPConnection._http_vsn = 10
> http.client.HTTPConnection._http_vsn_str = 'HTTP/1.0'
Python2 Version please add:
> import http.client
> http.client.HTTPConnection._http_vsn = 10
> http.client.HTTPConnection._http_vsn_str = 'HTTP/1.0'
See the reference How to deal with "http.client.IncompleteRead: IncompleteRead(0 bytes read)" problem

Resources