我用python写了一个简单的程序:
// main.py
import re
links = re.findall('(https?://\S+)', 'http://google.pl http://youtube.com')
print(links)
然后我执行以下命令:
cython main.py
它生成了一个文件:main.c然后我尝试了这个:
gcc main.c
我有一个错误:
main.c:8:10: fatal error: 'pyconfig.h' file not found
#include "pyconfig.h"
^
1 error generated.
如何将python编译为c?如何在Mac上使用带有xcode的cython入门?
您必须使用该标志告诉gcc
编译器pyconfig.h
系统上的文件在哪里-I
。您可以使用该find
程序找到它。
编译模块的一种更简单的方法是使用setup.py
模块。Cython提供了cythonize
启动.pyx
模块此过程的功能。
缺少的另一点是,Cython文件通常定义要在主要Python模块中使用的辅助函数。
假设您对目录和文件进行了以下设置:
cython-start/
├── main.py
├── setup.py
└── split_urls.pyx
的内容setup.py
是
from distutils.core import setup
from Cython.Build import cythonize
setup(name="My first Cython app",
ext_modules=cythonize('split_urls.pyx'), # accepts a glob pattern
)
该split_urls.pyx
文件的内容是
import re
def do_split(links):
return re.findall('(https?://\S+)', links)
它是使用定义的Cython函数的main.py
模块:
import split_urls
URLS = 'http://google.pl http://youtube.com'
print split_urls.do_split(URLS)
通过发出以下命令来编译Cython模块:
$ python setup.py build_ext --inplace
Cythonizing split_urls.pyx
running build_ext
building 'split_urls' extension
creating build
creating build/temp.macosx-10.9-x86_64-2.7
... compiler output ...
并检查您的主模块正在执行应做的事情:
$ python main.py
['http://google.pl', 'http://youtube.com']
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句