Embed Python3 without standard library
<p>EDIT: I have asked an opposing question here: <a href="https://stackoverflow.com/questions/34724879/how-to-embed-python3-with-the-standard-library">How to embed Python3 with the standard library</a></p>

<p>A solution for Python2 is provided here: <a href="https://stackoverflow.com/questions/20951624/is-it-possible-to-embed-python-without-the-standard-library">Is it possible to embed python without the standard library?</a></p>

<p>However, Python3 fails on <code>Py_Initialize();</code> with:</p>

<pre><code>Fatal Python error: Py_Initialize: unable to load the file system  
codec ImportError: No module named 'encodings'
</code></pre>

<p>This makes sense because py3 source files are utf-8 by default. So it seems that it requires an external binary just in order to parse py3 source files.</p>

<p>So what to do?</p>

<p>It looks as though I need to locate the <code>encodings</code> binary in my system Python installation, copy this into my project tree, and maybe set some environment variable PYTHONPATH(?) So that my libpython.dylib can find it.</p>

<p>Is it possible to avoid this? And if not, can anyone clarify the steps I need to take? Are there going to be any more hiccups?</p>

<hr>

<p>NOTE: For posterity, this is how I got a stand-alone libpython.dylib linking into my project on OSX:</p>

<p>First I locate my system Python's library: <code>/usr/local/Frameworks/Python.framework/Versions/3.4/Python</code> (in my case it was installed with homebrew).</p>

<p>Now I:</p>

<ul>
<li><p>copy the .dylib into my project folder creating <code>./Libs/libpython3.4.1_OSX.dylib</code></p></li>
<li><p>Go into <code>build settings -&gt; linking</code> and set <code>other linker flags</code> to <code>-lpython3.4.1_OSX</code>  </p></li>
</ul>

<p>At this point it will appear to work. However if you know try building it on a fresh OSX installation, it will fail. This is because:</p>

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

<p>The .dylib is still holding onto it's old location.  It's really weird to me that the .dylib contains a link to its location, as anything that uses it must know where it is in order to invoke it in the first place!</p>

<p>We can correct this with:</p>

<pre><code>$ install_name_tool -id @rpath/libpython3.4.1_OSX.dylib libpython3.4.1_OSX.dylib
</code></pre>

<p>But then also in our Xcode project we must:</p>

<ul>
<li>Go into <code>build phases</code>. Add a <code>copy files</code> step that copies <code>libpython3.4.1_OSX.dylib</code> to <code>Frameworks</code> (that's the right place to put it).  </li>
<li>Go into <code>build settings -&gt; linking</code> and set <code>runpath search paths</code> to <code>@executable_path/../Frameworks/libpython3.4.1_OSX.dylib</code>  </li>
</ul>

<p>Finally I need to go into <code>edit scheme -&gt; run -&gt; arguments -&gt; environment variables</code> and add <code>PYTHONHOME</code> with value <code>../Frameworks</code></p>

<p>I suspect that to get this working I will also need to add a <code>PYTHONPATH</code></p>

<p>Links:<br>
<a href="https://mikeash.com/pyblog/friday-qa-2009-11-06-linking-and-install-names.html" rel="nofollow noreferrer">https://mikeash.com/pyblog/friday-qa-2009-11-06-linking-and-install-names.html</a><br>
<a href="http://qin.laya.com/tech_coding_help/dylib_linking.html" rel="nofollow noreferrer">http://qin.laya.com/tech_coding_help/dylib_linking.html</a><br>
<a href="https://github.com/conda/conda-build/issues/279#issuecomment-67241554" rel="nofollow noreferrer">https://github.com/conda/conda-build/issues/279#issuecomment-67241554</a><br>
<a href="https://stackoverflow.com/questions/9263256/can-you-please-help-me-understand-how-mach-o-libraries-work-in-mac-os-x">Can you please help me understand how Mach-O libraries work in Mac Os X?</a><br>
<a href="http://nshipster.com/launch-arguments-and-environment-variables/" rel="nofollow noreferrer">http://nshipster.com/launch-arguments-and-environment-variables/</a>  </p>