Python embedding into Xojo app bundle

Hello,

I am starting to exploring possibility to use Xojo for building GUI frontends for Python modules and scripts.
As I don’t wont my final user to rely on system Python (that could not be available) I am wondering if there is a way to embed the Python interpreter and all needed Python packages in the bundle that Xojo builds.
On macOS, for example, the system Python3 is distributed wrapped in a Python.framework bundle with a libpython3.9.dylib inside it.

Thank you for any advice

dylibs can be copied into Contents/Frameworks. How do you want to talk to Python?

It’s been a while since I did some Python for Xojo. I compiled the Python code to a CLI app and then used the shell to talk to the app.

You might want to check out Einhugur Software - Plugin Script Engines for Xojo which offers support for Python.

thank you for the quick answer. I am actually using the Plugin3Script from Einhugur.
How did you compile and package your python CLI app? using py2app?

Could you give me additional directions on how to copy into Contents/Frameworks please?
I see I can add a “Build Step” with “Copy Files” actions and I can choose either Contents or Framework folder. Which one should I choose or I should put into both? And where is the best location for my python modules?

thanks

I would have to check what I used. It could have been py2app.

The correct location for the dylib is the Frameworks folder. If you mess up you will get an error on notarisation.

Do you plan to have examples? The examples should be placed in the app for distribution. When the user opens the app you can copy them to a location in the documents.

Thank you! I was able to include both the Python.framework and the Python modules in the Frameworks and Resource folder respectively (using a Build step as per you suggestion):

Now I need to refer to that directory by code. Is there any Xojo method or property to get a reference to the Frameworks or Contents or Resource folder?
I am wondering also if the Resource folder has write permission, as the .py files should be compiled into .pyc (or maybe I can add directly the .pyc compiled version)

Hopefully I would not have trouble with app notarisation and be able to submit to mac app store.

Thank you for the help

Anything in the app does not have write permission at all. You MUST copy your files out of the app bundle.

Resources:

dim ResourcesFolder as FolderItem = SpecialFolder.Resources

Framework:

I only use a special dylib for some declares. I don’t know if your Python needs something similar:

Private Const CryptoLib as String
@executable_path/../Frameworks/libcrypto.0.9.8.dylib

And then I have declares like the following:

#if TargetMacOS or TargetLinux
  Declare Sub ERR_load_crypto_strings Lib CryptoLib ()
  ERR_load_crypto_strings
#endif
2 Likes