Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I load a pickle model when using cx_freeze?

My script, built as an application, stops working when it tries to load pickle model data. The following is the problematic code:

with open('model_pickle','rb') as f:
    mp = pickle.load(f)

This is the setup file I’m using:

setup(
    name="Test",
    version="1.0",
    options={"build_exe":{"include_files":["model_pickle"]}},
    executables=[target]
)

The program works as it should when executing the python script. However, after converting it to an executable via cx_freeze, the issue is with opening the pickle model data file. I have tested this with and without the data, so I’m pretty sure this is the issue.

like image 835
Sakib Shahriar Avatar asked Mar 23 '26 14:03

Sakib Shahriar


2 Answers

You can import model as follows model = pickle.load(open('MODEL_PATH','rb')) .Hope this will work!

like image 173
Ransaka Ravihara Avatar answered Mar 26 '26 04:03

Ransaka Ravihara


I’m 99.99% positive that you need to also add the model_pickle file to your setup(…) call as package data for this to work as an executable. Here’s a setup.py snippet showing the package data options:

from setuptools import setup, find_packages

PROJECT_NAME = 'my_project' # this should reflect your package structure

setup(
    # …
    packages=[package for package in find_packages() \
                       if package.startswith(PROJECT_NAME)],

    package_dir={ 'my_project' : 'my_project' },
    package_data={ '' : ['*.*'] },
    include_package_data=True,
    zip_safe=True,
    # …
)

Also, if I were you, I’d rename the data file to something like model_pickle.pkl so that your package_data expression doesn’t have to be double-wildcarded (as above).

If that doesn’t immediately work, I would also recommend adding a MANIFEST.in file that explicitly names your binary data file.

like image 24
fish2000 Avatar answered Mar 26 '26 03:03

fish2000



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!