从C调用Python脚本unableto load the file system codec ImportError错误解决方法
来源:互联网 发布:姚明09年季后赛数据 编辑:程序博客网 时间:2024/06/05 20:17
配置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源码:
// 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源码:
#!/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
a
+
b
二、解决办法:
在初始化Python解释器前使用Py_SetPythonHome来设置Python的根目录。
// 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写的模块文件。
// 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脚本,编译运行,一切正常,程序输出:
call python
from
c
a
=
4
b
=
5
ret
=
9
请按任意键继续. . .
三、后续补充(2013-07-19)
终于发现问题出现的原因了,原来是我先打开的Visual后配置的PYTHONHOME,导致的这个错误。重启Visual之后一切OK了,这也是在用Editplus跑一个脚本时碰到的,唉,多么熟悉而容易犯错的问题啊......牢记!!!
- 从C调用Python脚本unableto load the file system codec ImportError错误解决方法
- import win32api 出现ImportError: DLL load failed 错误的解决方法
- ImportError:The _imagingft C module is not installed 错误的解决方法
- PIL写入字体出现ImportError:The _imagingft C module is not installed 错误的解决方法
- java调用jython报ImportError: No module named...错误解决方法
- Boost Python 实现C调用python错误解决方法
- linux系统中:an error occurred during the file system check 错误解决方法
- Linux系统:an error occurred during the file system check 错误解决方法
- python错误 ImportError: No module named setuptools 解决方法
- Could not load file or assembly 'System.Web.Helpers...错误
- Python 的 ImportError 错误
- Greenlet ImportError: DLL load failed 解决方法
- python PIL ImportError: The _imagingft C module is not installed
- ImportError: The Python Imaging Library (PIL) is required to load data from jpeg files
- linux c使用system调用shell脚本
- C调用Python脚本代码
- c/c++ 调用python 脚本
- C语言调用PYTHON脚本
- handler引用的内存泄露
- 数组中的逆序对
- 深入java集合系列:HashMap 二
- 洛谷 [P1282] 多米诺骨牌
- js正则表达式判断电话号码是哪个运营商
- 从C调用Python脚本unableto load the file system codec ImportError错误解决方法
- kotlin学习笔记(三)
- PHP基础语法
- 第二讲 c++初步
- jquery 插入文本时不能插入转义字符
- Android扫一扫(Zxing),闪光灯,生成二维码图片,解析二维码(条码)等
- 【Oracle】授权导入和导出( IMP_FULL_ DATABASE ORA-00990 )
- .gitignore文件使用的误区
- golang make & new