Script .py reading fine but not after building with pyinstaller - python

When i run setup.py as a script i have no issues reading a parameter file.
When i build with pyinstaller and run the same script as a .exe i receive the below error.
>setup.exe
Traceback (most recent call last):
File "setup.py", line 106, in <module>
param_file_info = paramsfx.extract_param_file_info(param_text)
File "app\paramsfx.py", line 64, in extract_param_file_info
s_n = re.search(rc_n, param_file_text)
File "c:\users\xxxxxx\appdata\local\programs\python\python37-32\lib\re.py", line 183, in search
return _compile(pattern, flags).search(string)
TypeError: cannot use a string pattern on a bytes-like object
[21776] Failed to execute script setup
i have read up on other posts how to resolve this error however before changing the code (that is working fine as a script) i wanted to see if anyone had any thoughts as to why it read the parameter differently as a .exe.

Just from looking at this output and not the code I noticed this:
"TypeError: cannot use a string pattern on a bytes-like object"
If this is true, I would recommend doing some python type conversion. Here is a link to help you out
https://www.w3schools.com/python/trypython.asp?filename=demo_numbers_convert
Because I don't know how your code is structured, choosing which form of conversion you choose may affect your results so please be careful with that.
I hope my explanation could have provided some use to you.

Related

TypeError while using Openpyxl to read file

I was trying out Openpyxl, and wrote the following code:
from openpyxl import load_workbook, __version__
workbook = load_workbook(filename="Contacts.xlsx")
sheet = workbook.active
That's it, no other code.
I got the following error on running:
(TL;DR, due to line 2, I got
"TypeError: __init__() got an unexpected keyword argument 'extLst'")
"C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\Scripts\python.exe" "C:/Users/taijee/Documents/PythonAll/Python Scripts/newfile.py"
Traceback (most recent call last):
File "C:/Users/taijee/Documents/PythonAll/Python Scripts/newfile.py", line 2, in <module>
workbook = load_workbook(filename="Contacts.xlsx")
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\reader\excel.py", line 315, in load_workbook
reader.read()
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\reader\excel.py", line 279, in read
apply_stylesheet(self.archive, self.wb)
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\styles\stylesheet.py", line 192, in apply_stylesheet
stylesheet = Stylesheet.from_tree(node)
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\styles\stylesheet.py", line 102, in from_tree
return super(Stylesheet, cls).from_tree(node)
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\descriptors\serialisable.py", line 83, in from_tree
obj = desc.from_tree(el)
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\descriptors\sequence.py", line 85, in from_tree
return [self.expected_type.from_tree(el) for el in node]
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\descriptors\sequence.py", line 85, in <listcomp>
return [self.expected_type.from_tree(el) for el in node]
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\styles\fills.py", line 64, in from_tree
return PatternFill._from_tree(child)
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\styles\fills.py", line 102, in _from_tree
return cls(**attrib)
TypeError: __init__() got an unexpected keyword argument 'extLst'
Process finished with exit code 1
I've searched for similar problems and found one, but it was from 2014 so the solution given was to download version 2.0 of Openpyxl instead of 2.2.
The openpyxl version is 3.0.4, python version 3.8.3, and Contacts.xlsx definitely exists in the same folder.
Edit:
Making a new file had worked, but I recently made another program using openpyxl. It was working fine but it suddenly broke, giving an error very similar to the one above (I didn't record it at the time, but the last line TypeError: __init__() got an unexpected keyword argument 'extLst' definitely matched).
I had made no changes to the program between the successful runs and the unsuccessful ones, so I concluded the file was at fault. I had opened and closed the file once, though I don't remember whether the very next run was the one when it broke. After the error appeared, when I opened the file I couldn't save any changes - it gave some sort of error.
Note: I don't have Office, so I'm using Planmaker of the SoftMaker Office Suite to open the .xlsx files when I need to.
I deleted the file and created a new one with the same name so as to not change the code, but the problems persisted with the new file, including the inability to save changes.
Only when I created a new file with a different name did the code work.
It may not even be an openpyxl problem, but rather a Planmaker problem, though I don't understand why the error would be the one I keep getting.
Neverthless, If someone could explain why this happens or how to fix it, I'll be really grateful.
Meanwhile, I'll see if a file I never use openpyxl on will still give me the same problem, and if it does, whether openpyxl gives the same error with that file when used in a program.
I too was using Planmaker of the SoftMaker Office Suite, got the same error when I try to edit one of my .xlsx with openpyxl. The only solution that I found is by not using the Planmaker and go back to MS Excel.
As much as I don't like MS Excel like the next guy, but it works flawlessly with openpyxl.
Probably not the solution you seek, but it's a solution for me at least.
I got the same error "
TypeError: init() got an unexpected keyword argument 'extLst'
" because I changed and saved a tabular workbook file in xlsx format with Planmaker software.
Thereafter I saved this file with libreoffice calc. Then loading of this xlsx file with openpyxl was working.
This might be a workaround for some users.
Just a note, in case others stumble over this: openpyxl in the latest version (3.0.10) seems to now raise an error on the "unexpected keyword". Before you could using the warnings-module to ignore it. No more.
Since I am working on Linux, going to MS Excel is not an option, so I rewrote my latest Python project to use pylightxl instead of openpyxl. It more hassle, as it has a lot less functionality, but it does not choke on Softwaker's extensions.
– Hendrik

Python script - file operations

I am trying to fetch the free disk space details of each drive using Python Scripts and storing the values in a variable 'p'.
p=(disk_usage('C:/'))
f=open('C:\\Python27\\solr.log','a')
f.write("Solr "+p)
I would like to place the results from variable 'p' into a log file. If I print the variable 'p', I am able to get the results successfully. But when I try to open a log file and write into it, it is throwing the below error.
"Traceback (most recent call last):
File "C:\Python27\sample.py", line 51, in <module>
f.write("Result: "+p)
TypeError: must be str, not UsageTuple"
Could anyone please help me on this.
I got the answer. Just changed the 'p' variable to string and then I tried to write into file. I am able to get the results in my log file.

DecodeError: Error parsing message for .binaryproto conversion to array

I'm currently trying to convert the .binaryproto file here to a usable numpy array. I'm running everything in my python terminal and following some of the guides as given here.
I can make it as far as seen below:
import caffe
blob = caffe.proto.caffe_pb2.BlobProto()
data = open('ucf101_train_mean.binaryproto','rb').read()
blob.ParseFromString(data)
At which point I get the error:
Traceback (most recent call last)L
File "<stdin>", line 1, in <module>
google.protobuf.message.DecodeError: Error parsing message
I've cleared and reinstalled caffe thinking it was an installation problem and it hasn't helped. I printed the data string and checked the length and both seem appropriate.
Or, as an alternative solution - is there another way I could potentially load the values of the .binaryproto file to get a usable mean? Thank you!

Understanding Parsing Error when reading model file into PySD

I am receiving the following error message when I try to read a Vensim model file (.mdl) using Python's PySD package.
My code is:
import pysd
import os
os.chdir('path/to/model_file')
model = pysd.read_vensim('my_model.mdl')
The Error I receive is:
Traceback (most recent call last):
Python Shell, prompt 13, line 1
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pysd/pysd.py", line 53, in read_vensim
py_model_file = translate_vensim(mdl_file)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pysd/vensim2py.py", line 673, in translate_vensim
entry.update(get_equation_components(entry['eqn']))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pysd/vensim2py.py", line 251, in get_equation_components
tree = parser.parse(equation_str)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/parsimonious/grammar.py", line 123, in parse
return self.default_rule.parse(text, pos=pos)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/parsimonious/expressions.py", line 110, in parse
node = self.match(text, pos=pos)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/parsimonious/expressions.py", line 127, in match
raise error
parsimonious.exceptions.ParseError: Rule 'subscriptlist' didn't match at '' (line 1, column 21).
I have searched for this particular error and I cannot find much information on the failed matching rule for 'subscriptlist'.
I appreciate any insight. Thank you.
Good news is that there is nothing wrong with your code. =) (Although you can also just include the path to the file in the .read_vensim call, if you don't want to make the dir change).
That being the case, there are a few possibilities that would cause this issue. One is if the model file is created with a sufficiently old version of Vensim, the syntax may be different from what the current parser is designed for. One way to get around this is to update Vensim and reload the model file there - Vensim will update to the current syntax.
If you are already using a recent version of Vensim (the parser was developed using syntax of Vensim 6.3E) then the parsing error may be due to a feature that isn't yet included. There are still some outstanding issues with subscripts, which you can read about here and here).
If you aren't using subscripts, you may have found a bug in the parser. If so, best course is to create a report in the github issue tracker for the project. The stack trace you posted says that the error is happening in the first line of the file, and that the error has to do with how the right hand side of the equation is being parsed. You might include the first few lines in your bug report to help me recreate the issue. I'll add a case to our growing test suite and then we can make sure it isn't a problem going forwards.

TypeError: expected a character buffer object while translating

I am working on a project that uses an sqlite3 database to store some of the data.
My search function uses the SQL statement: '''SELECT text FROM snippets WHERE title=?''', (whichName,) and as my code was, whichName came in as a dictionary, which garnered this error:
Traceback (most recent call last):
File "snippets.py", line 93, in <module>
main()
File "snippets.py", line 24, in main
get_value_from_name(response)
File "snippets.py", line 58, in get_value_from_name
(whichName,))
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
Thus, I figured I needed to pass it as a string, so I just did name = str(response) to convert it to a string, but here is where the problem began. It gave me this:
[u'TEST'] <--- What is returned by the conversion to a string
None <--- What is returned by the search function
when I converted the dictionary to a string. The search function then returned None because it was being passed [u'TEST'] instead of TEST like it should have been. So, I added some translation code:
translation_table = dict.fromkeys(map(ord, '(),'), None)
# Above code creates a table with the mapped characters
name = str(response)
name = name.translate(translation_table)
This is where my current problem is. It is returning the following error:
Traceback (most recent call last):
File "snippets.py", line 93, in <module>
main()
File "snippets.py", line 20, in main
name = name.translate(translation_table)
TypeError: expected a character buffer object
I looked at these questions:
Python TypeError: expected a character buffer object, personal misunderstanding - not applicable to my problem
expected buffer object error on string.translate - python 2.6 - also not applicable, as my translation table is created from a string not a dictionary
Getting error "expected character buffer object" and I don't know why - also not applicable because he is trying to do it at an index. I'm trying to do it like it should, finding/replacing throughout the entire string.
but none are applicable to my issue (as far as I can see.)
Does anyone know what is causing the Type Error?
Thanks!
I solved this. I ran the code in Python 3.3.2+ and it works find (albeit a few minor tweaks to the translation table)
Sorry if I wasted anyone's time. I will mark this as answer as soon as I can.

Categories

Resources