c++ – Py_Initialize fails – unable to load the file system codec

c++ – Py_Initialize fails – unable to load the file system codec

Check the PYTHONPATH and PYTHONHOME environment variables and make sure they dont point to Python 2.x.


Parts of this have been mentioned before, but in a nutshell this is what worked for my environment where I have multiple Python installs and my global OS environment set-up to point to a different install than the one I attempt to work with when encountering the problem.

Make sure your (local or global) environment is fully set-up to point to the install you aim to work with, e.g. you have two (or more) installs of, lets say a python27 and python33 (sorry these are windows paths but the following should be valid for equivalent UNIX-style paths just as well, please let me know about anything Im missing here (probably the DLLs path might differ)):



Now, if you intend to work with your python33 install but your global environment is pointing to python27, make sure you update your environment as such (while PATH and PYTHONHOME may be optional (e.g. if you temporarily work in a local shell)):




Note, that you might need/want to append any other library paths to your PYTHONPATH if required by your development environment, but having your DLLs, Lib and site-packages properly set-up is of prime importance.

Hope this helps.

c++ – Py_Initialize fails – unable to load the file system codec

The core reason is quite simple: Python does not find its modules directory, so it can of course not load encodings, too

Python doc on embedding says Py_Initialize() calculates the module search path based upon its best guess … In particular, it looks for a directory named lib/pythonX.Y

Yet, if the modules are installed in (just) lib – relative to the python binary – above guess is wrong.

Although docs says that PYTHONHOME and PYTHONPATH are regarded, we observed that this was not the case; their actual presence or content was completely irrelevant.

The only thing that had an effect was a call to Py_SetPath() with e.g. [path-to]lib as argument before Py_Initialize().

Sure this is only an option for an embedding scenario where one has direct access and control over the code; with a ready-made solution, special steps may be necessary to solve the issue.

Leave a Reply

Your email address will not be published. Required fields are marked *