Python Sqlite3 not being found when packaged into an app through Py2App

126 views Asked by At

I made a stock management system using sqlite3 for the database and customtkinter for the GUI. The code works as intended when ran through VSCode. I am on MacOS Ventura on the apple silicon. I want to send the code as an app so it can be convenient for the end user.

After using py2app to package the code, the alias version and the deployment version runs as intended on the Dist directory. However when I take the code out of Dist, for example into my desktop, it runs into a launch error.

ImportError: dlopen(/Users/name/Desktop/stockmanager.app/Contents/Resources/lib/python3.11/lib-dynload/_sqlite3.so, 0x0002): Symbol not found: _sqlite3_enable_load_extension
  Referenced from: <8B6D5BCF-C215-35A7-B03F-EDE59AAF1BC3> /Users/name/Desktop/stockmanager.app/Contents/Resources/lib/python3.11/lib-dynload/_sqlite3.so
  Expected in:     <AB447288-47E6-30D9-BD4C-BDFDAE0CBBC3> /usr/lib/libsqlite3.dylib

From what I can understand it fails to find the Sqlite3 library. I installed python3.11 using homebrew and sqlite3 installs with that as default. Still I tried installing sqlite3 directly through homebrew. I even downloaded the sqlite3.dylib from the official website to insert into the package contents, but I don't know where I would do that.

Since the code runs on VSCode and in the Dist directory normally, I can assume Sqlite3 is installed correctly on the system. I realise I could add the 'sqlite3.dylib' into the 'usr/lib/' directory but that feels like it would only fix it on this specific system. How can I package this correctly?

1

There are 1 answers

0
Ishaan Masil On

Just to clarify, are you using a venv (virtual environment) inside of the VSCode project? If so, I might try using cxFreeze as I've personally had much more luck getting it to work consistently. You simply need to add a setup.py file to the root directory, then put this in there:

from cx_Freeze import setup,Executable

include_files: [str] = [<files to include>]

options = {
    'build.exe': {
        'include_files':include_files
    }
}

setup(
    name="<title>",
    version=<version in float (like 0.1)>,
    description="<description>",
    executables=[Executable("<path to main from root>", base="Win32GUI")],
    options=options
)

Then run python setup.py build from the root directory, and you should have. build folder with a functioning .exe file.

If, for some reason, you don't have a venv setup, your first order of business is definitely going to be to get that venv setup because it makes this whole process so much easier. Here is a pretty good guide: https://code.visualstudio.com/docs/python/environments

If you have any more questions feel free to comment and I'll try to help out.