AttributeError: Can't get attribute 'Project' on <module '__main__' (built-in)>

Hi there, I am currently running generic worker and having trouble loading the output file generated by the worker. Here’s the code and error:

import pickle
from viktor.external.generic import GenericAnalysis
from utils_2 import Project, UserInputs
user_inputs = UserInputs(soil_profile, footing_input, variables)
files = [("user_inputs.pickle", BytesIO(pickle.dumps(user_inputs)))]

generic_analysis = GenericAnalysis(
    files=files,
    executable_key="substratum_worker",
    output_filenames=["project.pickle"],
)
generic_analysis.execute(timeout=6000)
output_file = generic_analysis.get_output_file("project.pickle")
project = pickle.load(output_file)
Traceback (most recent call last):
  File "viktor_connector/connector.pyx", line 295, in connector.Job.execute
  File "viktor/core.pyx", line 1918, in viktor.core._handle_job
  File "viktor/core.pyx", line 1864, in viktor.core._handle_job._handle_button
  File "/home/user/src/app.py", line 867, in run_optimizer
    project = pickle.load(output_file)
              ^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: Can't get attribute 'Project' on <module '__main__' (built-in)>

Hi MCAN,

Thank you for your patience. Looking into this, I am suspecting that it could be due to the object type you receive from the generic_analysis.get_output_file. As indicated by the documentation, the following types can be expected from the get_output_file method: BytesIO, File or None. I suspect that the output_file object is a BytesIO. As per the pickle documentation, the load method expects a file object. Therefore, I would check if this would work if you try the following:

with output_file.getbuffer() as f:
    project = pickle.load(f)

Hi mslootweg, unfortunately the suggested code change lead to this error.

Traceback (most recent call last):
  File "viktor_connector/connector.pyx", line 295, in connector.Job.execute
  File "viktor/core.pyx", line 1918, in viktor.core._handle_job
  File "viktor/core.pyx", line 1864, in viktor.core._handle_job._handle_button
  File "/home/user/src/app.py", line 868, in run_optimizer
    project = pickle.load(f)
              ^^^^^^^^^^^^^^
TypeError: file must have 'read' and 'readline' attributes

Hi there, I found a post that addresses and solves the issue above.

Effectively, it’s due to the way the pickle file stores the class instance in lazy approach and a more robust option is the dill pickle package

1 Like