Before I ask you, please understand that I am not good at English. im sorry.
import...
sharedMem_chk=mp.Value(ctypes.c_bool,False)
def all_loopStop(chk):
#print("[def]all_loopStop:::ready")
while True:
if chk.value==False:
if keyboard.is_pressed('q'):
print("::stop loop::")
chk.value=True
def __init__():
test1 = mp.Process(target=all_loopStop, args=(sharedMem_chk,))
test1.start()
if __name__ == '__main__':
__init__()
This is part of my code.
It works fine when compiling and debugging, but when I write an exe file with cx_freeze and run the exe file I get the following error message:
_pickle.PicklingError: Can't pickle : attribute lookup all_loopStop on main failed
After searching for an hour, I think the reason for the error is data not serialized, but the all_loopStop function has not needed serialized data, so I don't know why the error occurs.
The development environment is python3.7 32bit, and I'll also attach a detailed debug console window.
I need your help very much. I apologize once again for asking questions with my poor English.
Debug console screenshot
Related
I've seen a couple questions similar to this, but they both appear to involve VBA and not Python.
This is a relatively recent error, so I suspect it might have something to do with the fact that I'm using Python 3.7 now.
Basically, using the Dispatch method from win32com.client, I am able to open a new Excel workbook and make my edits as I always have been. However, for some reason, I am unable to tell the application to quit successfully.
Used to be that I could write:
self.excel_app.Quit()
But now, I'm getting an AttributeError, of all things. Saying the Excel.Application does not have a Quit() attribute. Again, this is Python 3.7. What happened?
[EDIT]
Relevant code:
import sys
from win32com.client import Dispatch
#...
class X(object):
def __init__(self):
#...
self.excel_app = Dispatch("Excel.Application")
self.report_workbook = self.excel_app.Workbooks.Add()
#...
def close_excel(self):
try:
self.excel_app.Quit()
except Exception as ex:
sys.stdout.write("Could not quit application.\n-> ({}) {}\n".format(ex.__class__.__name__, ex))
self.excel_app = None
The exception printed to the terminal is:
Could not quit application.
(AttributeError) Excel.Application.Quit
Turns out that my error was coming from the fact that I was still using threading.Thread as the base class for the object trying to do this. Apparently it doesn't work so well with the Dispatch Excel objects anymore, and I'm trying to move away from Threads anyway.
I am running PyCharm Community Edition 2021.1.3 and having this strange problem where I would run a file in one project and it would produce an error but run the same file in another project and it would produce no error. I have tried figuring out what is causing the problem but to no avail. I have discovered that any file with an import, in the first project (The one that gives me an error) would always get the error but a file without any imports works fine. This is the same whether the code after is correct or not. If someone could please help me with this problem as I could have missed something simple it would be much appreciated.
Code used in both projects:
from flask import Flask, render_template
from forms import RecommendationForm
app = Flask(__name__)
app.config['SECRET_KEY'] = '777'
#app.route('/')
def home():
form = RecommendationForm()
return render_template('index.html', form=form)
if __name__ == '__main__':
app.run()
Screenshot of project producing error:
Pycharm project with error
Screenshot of project working correctly:
Diffrent Pycharm project with the same code
The error tends to generally look the same with a few variations. Here is an example running different code but in the first project.
Diffrent code run in the error producing project
The rest of the error logs from the above example
I am connected to an Excel application and can execute the "Debug"->"Compile VBAProject" from my Python code using win32com like so (inspired by code from here):
from win32com import client
def compile(self):
self.__excel = client.GetActiveObject("Excel.Application")
compile_button = self.__excel.VBE.CommandBars.FindControl(1, 578)
compile_button.Execute()
If there is a compilation error in the Excel VBA code I get a popup message in Excel telling me the error just fine.
Now I would like to check from the Python code if there was a compilation error and raise an exception if there was. I don't necessarily need the compilation error to be part of the exception but if that were possible I would of course gladly take that, too.
Can this be done somehow?
I've been experimenting with all kinds of window counts before and after the compilation etc. but so far have not found a property of any object that would indicate that there was a popup or a compilation error.
Ok, I found a somewhat ugly but doable way - that I would like to document for others having the same issue:
You need to import a code file into the opened Excel file that has (at least) one function defined. Then you can call this function from your Python code and catch any exception. If there was an exception your code - including the imported file - did not compile, if there is none the compilation was pass.
Here's my code:
compile_code.bas
Public Sub compileCode()
' doesn't need to do anything, it just needs to be available!
End Sub
Python file
from win32com import client
def compile(self) -> bool:
self.__excel = client.GetActiveObject("Excel.Application")
self.__book = self.__excel.ActiveWorkbook
self.__book.VBProject.VBComponents.Import(<Path_to_compile_code.bas>)
try:
self.__excel.Application.Run("compileCode")
# if you reach here the code compiled
return True
except Exception:
return False
So I get that google colab crashes when using cv2.imshow for displaying images. Google colab has its own solution for replacing that function and google.colab.patches import cv2_imshow can be used as a replacement for displaying images.
However, I did notice that colab raises a DisabledFunctionError when I try using cv2.imshow. This led me to think that maybe I can try catching that error using 'Try and Except' block. But in order to do that DisabledFunctionError must be defined as a custom error in python. So I wrote an exception class to define that error:
class DisabledFunctionError(Exception):
pass
Now having done that I should assume that an error can be handled using try and except block as
follows:
try:
cv2.imshow(frame, image)
except DisabledErrorFunction:
print('Error handled')
But, to my surprise, colab still raises an exception and its not caught by the try and except block. This behavior seems strange to me. Am I missing something here? Is this behavior due to colab?
I have a python telegram bot and I have deployed it on Heroku. But problem is that my program actually creates pickled files while working. I have one database which is saving the required data and pickles to save some nested classes which I have to use later at some stage. So these pickle files are one of the important parts of the program. I am using the dill module for pickling.
I was able to save these files locally, but can't do when I am doing it in Heroku. I'll share the logs below. It is not even reaching the pickling part, but giving an error in opening the file itself.
import dill
def saving_test(test_path, test_obj):
try:
save_test_logger.info('Saving test...')
try:
save_test_logger.info('opening file')
test_file = open(test_path, 'wb')
except Exception as exc:
save_test_logger.exception('Error opening file')
return 0
dill.dump(test_obj, test_file)
save_test_logger.debug(f'file saved in {test_path}')
test_file.close()
return 1
except Exception as exc:
save_test_logger.exception('saving error')
test_file.close()
return exc
saving 859ab1303bcd4a65805e364a989ac8ca
2020-10-07T20:53:18.064670+00:00 app[web.1]: Could not open file ./test_objs/859ab1303bcd4a65805e364a989ac8ca.pkl
And I have added logging also to my program, but now I am confused about where can I see the original logs which are supposed to catch the exceptions also.
This is my first time using Heroku and I am comparatively new to programming also. So please help me out here to identify the route cause of the problem.
I found the problem. Even though I have pushed everything, directory test_objs wasn't there on Heroku. I just added 2 more lines of code to make a directory using os module if the directory does not exist. That solved the problem. I am not deleting the question so that just in case someone gets stuck or confused in a similar kind of situation, this question might be able to help them.