3

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.

fish2000
  • 4,289
  • 2
  • 37
  • 76
Sakib Shahriar
  • 121
  • 1
  • 12

3 Answers3

1

Have you tested your program with the IDLE ? Maybe there is a pickle decoding error... If not, try using this code for creating your exe. It imports all the libraries that you have on your computer. It's longer, but easier: just one file to run.


Anyways, better user PyInstaller (also a module available on PyPI) - Related Stack Overflow answer

D_00
  • 1,440
  • 2
  • 13
  • 32
1

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

Ransaka Ravihara
  • 1,786
  • 1
  • 13
  • 30
1

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.

fish2000
  • 4,289
  • 2
  • 37
  • 76