Confusion between Python and Xojo

At some days I don’t like developing very much. I’ve got a tiny little Python helper app that fixes screwed up encodings. The code works fine in the PyCharm IDE. It also works fine when executing the built app from terminal. When using the shell in Xojo to execute the app I get

[quote]Fatal Python error: Py_Initialize: unable to load the file system codec
zipimport.ZipImportError: can’t decompress data; zlib not available

Current thread 0x00007fff72d82000 (most recent call first):
Abort trap: 6[/quote]

Goggle tells me that this is a problem between different Python installations: “Check PYTHONPATH and PYTHONHOME system variable and make sure it doesn’t points to Python 2.x”

The Xojo docs tell me that a Xojo shell is not the normal shell. Can I change PYTHONPATH and PYTHONHOME dynamically for the Xojo shell to reflect the path to the Python in the app?

The code needs a library that needs Python 3 so that using the Einhugur plugin for the Python code is not possible.

You can set them in the same command you launch python.

PYTHONPATH=/path/goes/here;PYTHONHOME =/path/goes/here;python command

There is Einhugur Plugin for Python 3 also. (we have 2 sets, one for Python 2 and one for Python 3)

@Greg O’Lone : thanks, I’ll try this.

@Björn Einhugur: can I use the plugin at the customer without having Python 3 installed? For myself this is no problem. I can’t have my users install Python.

Now the usual stupid question: which values for path and home do I need? The Frameworks or the Resources folder in my app?

The value of PYTHONPATH in PyCharm is:
‘PYTHONPATH’: ‘/Users/beatrixwillius/PycharmProjects/ftfy:/Applications/Develop/PyCharm CE.app/Contents/helpers/pydev:/Users/beatrixwillius/Library/Caches/PyCharmCE2018.2/cythonExtensions’,

Go to a terminal and type

which python

The result is: /usr/bin/python

But this is on my computer. I have a built app - with Py2App - that has it’s own version of Python so that the users don’t have to install Python.

There is also no Python command. I have a very simple shell command:

dim PythonShell as new Shell PythonShell.Mode = 0 PythonShell.Execute(PathToPython.ShellPath + " " + tempFile.ShellPath)

Ok, so then use

PathToPython.Parent.ShellPath

No success:

PythonShell.Execute("PYTHONPATH='" + PathToPython.Parent.ShellPath + "';PYTHONHOME ='" + PathToPython.Parent.ShellPath + "';" + PathToPython.ShellPath + " " + tempFile.ShellPath)

[quote]bash: PYTHONHOME: command not found
Fatal Python error: Py_Initialize: unable to load the file system codec
zipimport.ZipImportError: can’t decompress data; zlib not available

Current thread 0x00007fff72d82000 (most recent call first):
bash: line 1: 78995 Abort trap: 6 /Users/beatrixwillius/Documents/Development/Mail\ Archiver/code\ current/Mail\ Archiver\ X.debug.app/Contents/Library/Helpers/fix_encoding.app/Contents/MacOS/fix_encoding /private/var/folders/yr/t1mywwrs1s1d0qv8cwv928240000gn/T/TemporaryItems/com.mothsoftware.mailarchiverx\-temp11
[/quote]

Then I tried without PYTHONHOME:

PythonShell.Execute("PYTHONPATH='" + PathToPython.Parent.ShellPath + "';" + PathToPython.ShellPath + " " + tempFile.ShellPath)

[quote]Fatal Python error: Py_Initialize: unable to load the file system codec
zipimport.ZipImportError: can’t decompress data; zlib not available

Current thread 0x00007fff72d82000 (most recent call first):
bash: line 1: 79609 Abort trap: 6 /Users/beatrixwillius/Documents/Development/Mail\ Archiver/code\ current/Mail\ Archiver\ X.debug.app/Contents/Library/Helpers/fix_encoding.app/Contents/MacOS/fix_encoding /private/var/folders/yr/t1mywwrs1s1d0qv8cwv928240000gn/T/TemporaryItems/com.mothsoftware.mailarchiverx\-temp12
[/quote]

Don’t use single quotes.

[quote=399956:@Beatrix Willius]@Greg O’Lone : thanks, I’ll try this.

@Björn Einhugur: can I use the plugin at the customer without having Python 3 installed? For myself this is no problem. I can’t have my users install Python.[/quote]

As far as I know then you always need to install Python to run any sort of Python. (Including when running it from Xojo)

Unless the OS has it installed of course.

I see that zlib is not available. Do your app supply zlib? I can’t find zlib. Does Python install zlib?

@Greg O’Lone : no change.

@Björn Eiríksson : that’s what Py2App or PyInstaller are for. They make a sort of runtime environment for Python and bundle this into an application.

@Alberto de Poo: the zlib is not the problem. Executing the same command

/Users/beatrixwillius/Documents/Development/Mail\\ Archiver/Classes/Build\\ Automation/fix_encoding.app/Contents/MacOS/fix_encoding /Users/beatrixwillius/Desktop/filemaker.eml

from Terminal and not from within the Xojo shell works fine.

The path to zlib could be the problem. The path from Terminal and the path from Xojo shell are different.

Edit: where is zlib on your system?

@Alberto de Poo: the zlib is in the app. The app thinks that Python is somewhere else and can’t find the zlib. But I don’t know how to point the app to the correct Python version.

Ok, so how about executing bash yourapp instead of your app directly?

Like Kem commented here: https://forum.xojo.com/48796-shell-in-high-sierra-10-13-5/p1#p396129

You might be able to put simply the Python directory into your Xojo App. (I have not tried it)

You can certainly do that with PyPy.

@Alberto de Poo: that certainly looks perverted but less so than the ideas I was toying with. For instance, I can do a “do shell script” from an AppleScript. But this needs a fixed script. And I read about dockerizing Python (shudder).

@Steve Wilson and @Björn Eiríksson : I’ll keep copying Python into my app in mind.