Here's a CPython program that tries to initialize the interpreter with an empty sys.path
:
#include <Python.h>
int main(int argc, char** argv)
{
wchar_t* program = NULL;
wchar_t* sys_path = NULL;
Py_NoSiteFlag = 1;
program = Py_DecodeLocale(argv[0], NULL);
Py_SetProgramName(program);
sys_path = Py_DecodeLocale("", NULL);
Py_SetPath(sys_path);
Py_Initialize();
PyMem_RawFree(program);
PyMem_RawFree(sys_path);
Py_Finalize();
}
Executing the program above raises the following error:
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
Current thread 0x00007ffff7fc6700 (most recent call first):
Signal: SIGABRT (Aborted)
So which of the packages and modules in the Python 3.5 standard library, besides the encodings
package, are absolutely required to run the Python 3.5
interpreter? This information seems to me absent from the documentation.
These are packages/modules that are used during interpreter start-up (as, @Charles Duffy noted in a comment, by looking in
sys.modules
).The result depends on whether you have
site
enabled or not (yourPy_NoSiteFlag = 1;
implies this isn't the case but anyway, I'll give both options :-)).site
drags a couple of additional modules with it when you use it like_sitebuiltins
andstat
, in total you could run Python using only the following:with
site
disabled, you're stripped down to the following6
:when invoked through
C
withPy_Initialize()
(or through Windows based on your comment) I'm guessingos.py
might not be actually needed.If you run the interpreter as Charles Duffy suggests in his comment, you'll load packages like
readline
. It has been a decade since I did this but IIRC you don't need that module if you use python as an extension to your C program, as there is no commandline interaction. The same might hold for other modules.The quickest way to determine what is really needed with only a slight chance of getting in too much is by putting all of lib/python3.5 where your program can find it, and in the program print out
sys.modules
, that will give you a list of what your program actually loaded, not what the interpreter might need to start up. After that remove everything not on that list.Here's another approach - asking the Python interpreter what modules are loaded:
_bootlocale
is not required but recommended. It's used for initializing the best encoding for sys.stdin/sys.stdout/sys.stderr. See https://hg.python.org/cpython/rev/fbbf8b160e8dsys.modules
can lie as it's mutable.