I am trying to run the Embedding Python in Another Application example found at https://docs.python.org/3/extending/embedding.html#very-high-level-embedding on my MacOS. While I can get the program to compile with my Anaconda distribution of Python3.7 and SCons, running it always throws the following error:
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
I've tried the advice given at this StackOverflow post How can I troubleshoot Python "Could not find platform independent libraries <prefix>", by setting my PYTHONHOME variable to /usr/local, my Anaconda root directory, and the parent directory of the Python executable. I've also tried setting my PYTHONPATH variable to each of the folders. However, setting PYTHONHOME will cause my Anaconda installation of Python to crash with the message:
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
I don't have any Python virtual environments set up and Python runs fine outside of this embedding example and setting the PYTHONHOME variable. Additionally the parent folder of the Anaconda Python binary is the first folder in my PATH variable, so I know Py_Initialize won't find the default MacOS installation of Python first. I've tried uninstalling and reinstalling Anaconda but the same error persists. The following code is my SConstruct file used to compile the example.
env = Environment(
CPPPATH=[
"/Users/user/anaconda3/include",
"/Users/user/anaconda3/include/python3.7m"
],
LIBPATH=["/Users/user/anaconda3/lib/python3.7/config-3.7m-darwin"],
LIBS=["dl", "python3.7m"]
)
env.Program("wrapper", "wrapper.c")
Any help or advice is appreciated. Thank you.