运行Python 3.5解释器需要哪些标准库模块?


问题内容

这是一个CPython程序,尝试使用空初始化初始化解释器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();
}

执行上面的程序会引发以下错误:

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)

那么,除了encodings包之外,Python
3.5标准库中的哪个包和模块绝对是运行Python3.5解释器所必需的?在我看来,文档中没有此信息。


问题答案:

这些是在解释器启动过程中使用的软件包/模块(例如,@Charles Duffy在注释中通过查看指出sys.modules)。

结果取决于您是否site启用了(您的Py_NoSiteFlag = 1;意思不是这样,但是无论如何,我将同时给两个选项:-))。

site当您像_sitebuiltins和一样使用它时,还拖动了几个其他模块stat,总共只能使用以下命令运行Python:

abc.py               encodings       os.py         _sitebuiltins.py  sysconfig.py
codecs.py            genericpath.py  posixpath.py  site.py           _collections_abc.py  
io.py                stat.py         _weakrefset.py

site残疾,你剥夺了以下内容6

abc.py  codecs.py  encodings  io.py  os.py  _weakrefset.py

当通过(或根据您的评论通过Windows)调用时CPy_Initialize()我猜测os.py可能实际上并不需要。