0

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.

scruffaluff
  • 345
  • 4
  • 20
  • Did you read the section 1.6 from your python documentation link: https://docs.python.org/3/extending/embedding.html#compiling-and-linking-under-unix-like-systems. It seems like you might need some more CFLAGS and LDFLAGS. – dmoody256 Feb 08 '19 at 20:20
  • So it turns out that setting LD_LIBRARY_PATH=~/anaconda3/lib/python3.7/config-3.7m-darwin solves the problem. Can you explain to me what argument I should pass to Environment to avoid having to alter LD_LIBRARY_PATH? – scruffaluff Feb 09 '19 at 05:53

1 Answers1

0

It will depend on how the python libraries are setup. I am assuming that they are using RPATH which in that case you can make your environment like this:

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"],
    LINKFLAGS='-rpath /Users/user/anaconda3/lib/python3.7/config-3.7m-darwin'
)

env.Program("wrapper", "wrapper.c")

in that config folder there should be some dynamic libraries, which you can run the otool on:

otool -L libpython3.7.dylib

Which should print how the library is being linked by the linker.

You can find more information here:

https://stackoverflow.com/a/31824839/1644736 https://wincent.com/wiki/@executable_path,_@load_path_and_@rpath

dmoody256
  • 538
  • 4
  • 12