Py_SetProgramName() and prefixes

Marko Havu marko.havu at jyu.fi
Thu Aug 28 05:58:25 EDT 2014


Hi,

I have a problem with Py_Initialize() not being able to find python in the path. Unless I use Py_SetProgramName() with the full path of the python interpreter, I get the following error message:
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: Py_Initialize: unable to load the file system codec
ImportError: No module named ’encodings’

Is there a way of getting this to work without setting $PYTHONHOME? I am sure this worked for me before. The odd part is, I haven’t touched my python installation or environment variables. While doing some testing, I stumbled across some weird behavior on Python 3.3 and 3.4: Py_GetPrefix(), Py_GetPath() and other functions that return paths that are supposed to be set by Py_SetProgramName() all return just ”/”:

---------------- main.c ----------------
#include <Python.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, const char * argv[])
{
   printf("path: %s\n", getenv("PATH"));
   Py_SetProgramName(L"/anaconda/envs/python3.4/bin/python");
   Py_Initialize();
   wprintf(L"prefix: %s\n", Py_GetPrefix());
   PyRun_InteractiveLoop(stdin, "<stdin>");
   Py_Finalize();

   return EXIT_SUCCESS;
}
----------------

Python 3.4 output (same with 3.3):

path: /anaconda/envs/python3.4/bin:/usr/bin:/bin:/usr/sbin:/sbin
prefix: /
>>> import sys
>>> sys.prefix
'/anaconda/envs/python3.4'


Python 2.7 output (setting correct path and changing char strings to wchar_t strings):

path: /anaconda/envs/python2.7/bin:/usr/bin:/bin:/usr/sbin:/sbin
prefix: /anaconda/envs/python2.7
>>> import sys
>>> sys.prefix
'/anaconda/envs/python2.7’

Am I doing something wrong, or is this a bug in Python 3?

Kind regards,
 Marko Havu


More information about the Python-list mailing list