python扩展错误
来源:互联网 发布:淘宝店铺头像制作logo 编辑:程序博客网 时间:2024/06/06 17:10
Windows下python扩展编程出现的错误解决
错误1:当在cmd命令下输入目录下的setup.py build,出现error: Unable to find vcvarsall.bat,但是本机上有这个文件,说是自动寻找的是vs9.0版本的,而我的是vs2010
错误2:针对错误1,在网上搜索的答案大都是将setup.py build 改成为setup.py build –compiler=mingw32或者setup.py build –c mingw32
,但是都出现gcc找不到错误,因为没有安装这个,太麻烦了,而且自己电脑上有vs
处理方法如下:
1.打开Visual Studio 2010命令行编译模式,你可以依次从开始菜单 – Microsoft Visual Studio 2010 – Visual Studio Tools – Visual Studio x64 Win64 命令提示(2010),如下图所示:
2.使用下面的命令设置环境:
set DISTUTILS_USE_SDK=1
set MSSdk=1
3.切换到所要编译的扩展setup.py所在路径运行下面的命令进行编译:
python setup.py install
(用install表示建立并且将代码包复制到python的安装环境,而用build只是在build\lib*中建立代码包,要直接使用得自己拷贝进python\Lib\site-packages中)一会儿后如果相关依赖没有错误,并且源代码正确的话应该可以成功编译!编译完成后你可以从build\lib*找到代码包,并且也已经安装进Python环境中去了。
如果编译出现问题,比如链接失败,你可以检查下环境设置是否有问题,比如系统Path变量是否包含C:\Python27\libs假设Python装在C盘。
setup.py如下:
#!/usr/bin/env python
from distutils.core import setup, Extension
MOD = 'Extest'
setup(name=MOD, ext_modules=[Extension(MOD, sources=['Extest.c'])])
Extest.c如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int fac(int n)
{
if (n < 2) return(1);
return (n)*fac(n-1);
}
char *reverse(char *s)
{
register char t,
*p = s,
*q = (s + (strlen(s) - 1));
while (s && (p < q))
{
t = *p;
*p++ = *q;
*q-- = t;
}
return s;
}
int test()
{
char s[BUFSIZ];
printf("4! == %d\n", fac(4));
printf("8! == %d\n", fac(8));
printf("12! == %d\n", fac(12));
strcpy(s, "abcdef");
printf("reversing 'abcdef', we get '%s'\n", reverse(s));
strcpy(s, "madam");
printf("reversing 'madam', we get '%s'\n", reverse(s));
return 0;
}
#include "Python.h"
static PyObject *
Extest_fac(PyObject *self, PyObject *args)
{
int num;
if (!PyArg_ParseTuple(args, "i", &num))
return NULL;
return (PyObject*)Py_BuildValue("i", fac(num));
}
static PyObject *
Extest_doppel(PyObject *self, PyObject *args)
{
char *orig_str;
char *dupe_str;
PyObject* retval;
if (!PyArg_ParseTuple(args, "s", &orig_str))
return NULL;
retval = (PyObject*)Py_BuildValue("ss", orig_str, dupe_str=reverse(strdup(orig_str)));
free(dupe_str);
return retval;
}
static PyObject *
Extest_test(PyObject *self, PyObject *args)
{
test();
return (PyObject*)Py_BuildValue("");
}
static PyMethodDef
ExtestMethods[] =
{
{ "fac", Extest_fac, METH_VARARGS },
{ "doppel", Extest_doppel, METH_VARARGS },
{ "test", Extest_test, METH_VARARGS },
{ NULL, NULL },
};
void initExtest()
{
Py_InitModule("Extest", ExtestMethods);
}
- python扩展错误
- python 安装扩展包错误整理
- python扩展
- Python 扩展
- 扩展python
- 扩展python
- 安装Python扩展时,Configure error: Python headers not found错误解决办法
- PHP扩展加载错误
- 《扩展和嵌入python解释器》1.2 Intermezzo: 错误和异常
- 用swig进行python扩展c语言(范例+错误解决)
- 利用boost.python 扩展python
- python扩展_swig例子
- Python 的扩展概述
- Python的C++扩展
- python 扩展相关话题
- Python一些扩展模块
- windows python pyrex 扩展
- Python扩展和嵌入
- jaxb 通过xml转ava对象
- WF4.0 基础篇 (二十七) WCF Workflow Service 在WCF中使用WF
- zedboard--ubuntu12.04(pc机下)下helloworld驱动从失败到成功过程
- #pragma warning (disable: 4786)
- 小水硕面试经验谈
- python扩展错误
- 阿里巴巴实习生招聘骰子题目
- WF4.0 基础篇 (二十八) WF调用PowerShell
- silverlight应用(一):ScrollViewer 文字滚动
- 自学考试之时间管理
- java程序员必知道的八大排序(二)
- 页面所有元素加载完成之后执行某个js函数
- java程序员必知道的八大排序之(三)
- struts2标签库struts-tags.tld的使用