Paste Python3 without standard library

EDIT: I asked the opposite question here: How to insert Python3 with a standard library

Here's a solution for Python2: Is it possible to implement python without a standard library?

However, Python3 does not work on Py_Initialize(); via:

 Fatal Python error: Py_Initialize: unable to load the file system codec ImportError: No module named 'encodings' 

This makes sense because the py3 source files are utf-8 by default. Thus, it seems that external analysis of the py3 files requires an external binary.

So what to do?

It seems I need to find the encodings binary in my Python system, copy it to my project tree and maybe set some PYTHONPATH (?) Environment variable so that my libpython.dylib can find it.

Can this be avoided? And if not, can someone clarify the steps I need to take? Will there be more hiccups?




NOTE. For posterity, this is how I got the standalone libpython.dylib associated with my OSX project:

First I host my Python library system: /usr/local/Frameworks/Python.framework/Versions/3.4/Python (in my case it was installed with homebrew).

Now my turn:

  • copy the .dylib file to the project folder by creating ./Libs/libpython3.4.1_OSX.dylib

  • Go to build settings -> linking and set other linker flags to -lpython3.4.1_OSX

At that moment it will work. However, if you know that you are trying to create it on a new OSX installation, it will fail. This is because:

 $ otool -D ./libpython3.4.1_OSX.dylib ./libpython3.4.1_OSX.dylib: /usr/local/Frameworks/Python.framework/Versions/3.4/Python 

.dylib still holds the old location. It is very strange to me that .dylib contains a link to its location, since everything that uses it must know where exactly in order to call it first!

We can fix this with:

 $ install_name_tool -id @rpath/libpython3.4.1_OSX.dylib libpython3.4.1_OSX.dylib 

But then in our Xcode project we should:

  • Go to build phases . Add a copy files step that copies libpython3.4.1_OSX.dylib to Frameworks (this is the place to put it).
  • Go to build settings -> linking and set runpath search paths to @executable_path/../Frameworks/libpython3.4.1_OSX.dylib

Finally, I need to go into edit scheme -> run -> arguments -> environment variables and add PYTHONHOME with the value ../Frameworks

I suspect that to complete this work I will also need to add PYTHONPATH

References:
https://mikeash.com/pyblog/friday-qa-2009-11-06-linking-and-install-names.html
http://qin.laya.com/tech_coding_help/dylib_linking.html
https://github.com/conda/conda-build/issues/279#issuecomment-67241554
Could you help me understand how Mach-O libraries work on Mac Os X?
http://nshipster.com/launch-arguments-and-environment-variables/

+48
python standard-library embedding
Jan 11 '16 at 14:35
source share
1 answer

I tried to do this, and it will take more time than you want to spend implementing Python 3 without the Python library.

Some modules in the library are required to run Python 3, and this will require many changes to work without them.

+2
Apr 14 '17 at 22:16
source share



All Articles