从C调用Python脚本unableto load the file system codec ImportError错误解决方法

本文介绍了在使用C调用Python代码时遇到的Fatal Python error问题,并提供了两种解决方案:一是使用Py_SetPythonHome设置Python根目录;二是使用Py_SetPath指定Python模块路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概要:

配置PYTHONHOME之前编译就会导致这个问题。重启Visual之后重新编译,一切OK。

转载:http://baike.xsoftlab.net/view/657.html 

碰到一个很诡异的问题,明明在系统变量中添加了PYTHONHOME,但是通过C来调用Python时报Fatal Python error: Py_Initialize: unableto load the file system codec ImportError: No module named 'encodings'错误,由unable to load the file system codec ImportError可知这个是路径问题。

一、问题描述

   环境:Python3.3,Visualize 2010,Win XP

   系统已配置了PYTHONHOME指向Python的安装目录,但是在编译之后运行时出错。

   C源码:


C++ | 复制
1
2
3
4
5
6
7
8
9
// filename:call_python_from_c  
#include "Python.h"  
int main(int argc, char** argv)  
{  
    Py_Initialize();  
    PyRun_SimpleString("from pytest import add\nadd(4,5)\n");  
    Py_Finalize();  
    return 0;  
}

Python源码:

Python | 复制
1
2
3
4
5
6
7
8
#!/usr/bin/python  
#filename:pytest.py  
def add(a,b):  
    print ("call python from c")  
    print ("a = " + str(a))  
    print ("b = " + str(b))  
    print ("ret = " + str(a+b))  
    return + b

二、解决办法:

   在初始化Python解释器前使用Py_SetPythonHome来设置Python的根目录。

C++ | 复制
1
2
3
4
5
6
7
8
9
10
// filename:call_python_from_c  
#include "Python.h"  
int main(int argc, char** argv)  
{  
    Py_SetPythonHome(L"D:/python/Python33");  
    Py_Initialize();  
    PyRun_SimpleString("from pytest import add\nadd(4,5)\n");  
    Py_Finalize();  
    return 0;  
}

   这里如果使用setPath也会报同样的错误Fatal Python error: Py_Initialize: unable to load thefile system codec ImportError: No module named 'encodings'。

   setPath也可以用来解决这个问题,不过需要注意的是setPath设置的是模块的路径,也就是如果想让我们正确的执行上面的程序,还必须需指明Python模块的路径也就是Python安装目录下的Lib路径。该路径下都是Python写的模块文件。

C++ | 复制
1
2
3
4
5
6
7
8
9
10
11
// filename:call_python_from_c  
#include "Python.h"  
int main(int argc, char** argv)  
{  
    Py_SetPath(L"D:/python/Python33/Lib;D:/python/study/call_python_form_C");  
    //Py_SetPythonHome(L"D:/python/Python33");  
    Py_Initialize();  
    PyRun_SimpleString("from pytest import add\nadd(4,5)\n");  
    Py_Finalize();  
    return 0;  
}

   上面程序中"D:/python/Python33/Lib"存放了Python的模块文件,"D:/python/study/call_python_form_C"存放了pytest.py脚本,编译运行,一切正常,程序输出:

Python | 复制
1
2
3
4
call python from c  
= 4  
= 5  
ret = 9

 

请按任意键继续. . .  

三、后续补充(2013-07-19)

   终于发现问题出现的原因了,原来是我先打开的Visual后配置的PYTHONHOME,导致的这个错误。重启Visual之后一切OK了,这也是在用Editplus跑一个脚本时碰到的,唉,多么熟悉而容易犯错的问题啊......牢记!!!

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值